下面列出了com.google.common.io.CharSource 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void test_write_standard_roundTrip() {
CharSource source =
ResourceLocator.ofClasspath("com/opengamma/strata/loader/csv/sensitivity-standard.csv").getCharSource();
ValueWithFailures<ListMultimap<String, CurveSensitivities>> parsed1 = LOADER.parse(ImmutableList.of(source));
assertThat(parsed1.getFailures().size()).as(parsed1.getFailures().toString()).isEqualTo(0);
assertThat(parsed1.getValue().size()).isEqualTo(1);
List<CurveSensitivities> csensList1 = parsed1.getValue().get("");
assertThat(csensList1).hasSize(1);
CurveSensitivities csens1 = csensList1.get(0);
StringBuffer buf = new StringBuffer();
WRITER.write(csens1, buf);
String content = buf.toString();
ValueWithFailures<ListMultimap<String, CurveSensitivities>> parsed2 =
LOADER.parse(ImmutableList.of(CharSource.wrap(content)));
assertThat(parsed2.getFailures().size()).as(parsed2.getFailures().toString()).isEqualTo(0);
assertThat(parsed2.getValue().size()).isEqualTo(1);
List<CurveSensitivities> csensList2 = parsed2.getValue().get("");
assertThat(csensList2).hasSize(1);
CurveSensitivities csens2 = csensList2.get(0);
assertThat(csens2).isEqualTo(csens1);
}
private Collection<Map<String, Object>> getFirstMatches() throws IOException {
CharSource charSource = backingStore.asByteSource().asCharSource(UTF_8);
try (Reader reader = charSource.openBufferedStream();
JsonInput input = json.newInput(reader)) {
input.beginObject();
while (input.hasNext()) {
String name = input.nextName();
if ("capabilities".equals(name)) {
input.beginObject();
while (input.hasNext()) {
name = input.nextName();
if ("firstMatch".equals(name)) {
return input.read(LIST_OF_MAPS_TYPE);
} else {
input.skipValue();
}
}
input.endObject();
} else {
input.skipValue();
}
}
}
return null;
}
private static Map<LocalDate, Map<CurveName, Curve>> parseCurves(
Predicate<LocalDate> datePredicate,
CharSource settingsResource,
Collection<CharSource> curvesResources) {
// load curve settings
Map<CurveName, LoadedCurveSettings> settingsMap = parseCurveSettings(settingsResource);
// load curves, ensuring curves only be seen once within a date
Map<LocalDate, Map<CurveName, Curve>> resultMap = new TreeMap<>();
for (CharSource curvesResource : curvesResources) {
Multimap<LocalDate, Curve> fileCurvesByDate = parseSingle(datePredicate, curvesResource, settingsMap);
// Ensure curve names are unique, with a good error message
for (LocalDate date : fileCurvesByDate.keySet()) {
Collection<Curve> fileCurves = fileCurvesByDate.get(date);
Map<CurveName, Curve> resultCurves = resultMap.computeIfAbsent(date, d -> new HashMap<>());
for (Curve fileCurve : fileCurves) {
if (resultCurves.put(fileCurve.getName(), fileCurve) != null) {
throw new IllegalArgumentException(
"Rates curve loader found multiple curves with the same name: " + fileCurve.getName());
}
}
}
}
return resultMap;
}
/***/
public static String getContentsFromFileEntry(final ZipEntry entry, String rootName) throws IOException,
URISyntaxException {
URL rootURL = Thread.currentThread().getContextClassLoader().getResource(rootName);
try (final ZipFile root = new ZipFile(new File(rootURL.toURI()));) {
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return root.getInputStream(entry);
}
};
CharSource charSrc = byteSource.asCharSource(Charsets.UTF_8);
return charSrc.read();
}
}
@Test
public void test_parse_multipleSources() {
CharSource source1 = CharSource.wrap(
"Reference,Sensitivity Tenor,Zero Rate Delta\n" +
"GBP-LIBOR,P1M,1.1\n" +
"GBP-LIBOR,P2M,1.2\n");
CharSource source2 = CharSource.wrap(
"Reference,Sensitivity Tenor,Zero Rate Delta\n" +
"GBP-LIBOR,P3M,1.3\n" +
"GBP-LIBOR,P6M,1.4\n");
ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER.parse(ImmutableList.of(source1, source2));
assertThat(test.getFailures().size()).as(test.getFailures().toString()).isEqualTo(0);
assertThat(test.getValue().keySet()).hasSize(1);
List<CurveSensitivities> list = test.getValue().get("");
assertThat(list).hasSize(2);
CurveSensitivities csens0 = list.get(0);
assertThat(csens0.getTypedSensitivities()).hasSize(1);
assertSens(csens0, ZERO_RATE_DELTA, "GBP-LIBOR", GBP, "1M, 2M", 1.1, 1.2);
CurveSensitivities csens1 = list.get(1);
assertThat(csens1.getTypedSensitivities()).hasSize(1);
assertSens(csens1, ZERO_RATE_DELTA, "GBP-LIBOR", GBP, "3M, 6M", 1.3, 1.4);
}
/**
* Checks whether the source is a CSV format sensitivities file.
* <p>
* This parses the headers as CSV and checks that mandatory headers are present.
*
* @param charSource the CSV character source to check
* @return true if the source is a CSV file with known headers, false otherwise
*/
public boolean isKnownFormat(CharSource charSource) {
try (CsvIterator csv = CsvIterator.of(charSource, true)) {
if (!csv.containsHeader(TENOR_HEADER) && !csv.containsHeader(DATE_HEADER)) {
return false;
}
if (csv.containsHeader(REFERENCE_HEADER) && csv.containsHeader(TYPE_HEADER) && csv.containsHeader(VALUE_HEADER)) {
return true; // standard format
} else if (csv.containsHeader(REFERENCE_HEADER) || csv.containsHeader(TYPE_HEADER)) {
return true; // list or grid format
} else {
return csv.headers().stream().anyMatch(SensitivityCsvLoader::knownReference); // implied grid format
}
} catch (RuntimeException ex) {
return false;
}
}
private static Multimap<LocalDate, Curve> parseSingle(
Predicate<LocalDate> datePredicate,
CharSource curvesResource,
Map<CurveName, LoadedCurveSettings> settingsMap) {
CsvFile csv = CsvFile.of(curvesResource, true);
Map<LoadedCurveKey, List<LoadedCurveNode>> allNodes = new HashMap<>();
for (CsvRow row : csv.rows()) {
String dateStr = row.getField(CURVE_DATE);
String curveNameStr = row.getField(CURVE_NAME);
String pointDateStr = row.getField(CURVE_POINT_DATE);
String pointValueStr = row.getField(CURVE_POINT_VALUE);
String pointLabel = row.getField(CURVE_POINT_LABEL);
LocalDate date = LoaderUtils.parseDate(dateStr);
if (datePredicate.test(date)) {
LocalDate pointDate = LoaderUtils.parseDate(pointDateStr);
double pointValue = Double.valueOf(pointValueStr);
LoadedCurveKey key = LoadedCurveKey.of(date, CurveName.of(curveNameStr));
List<LoadedCurveNode> curveNodes = allNodes.computeIfAbsent(key, k -> new ArrayList<>());
curveNodes.add(LoadedCurveNode.of(pointDate, pointValue, pointLabel));
}
}
return buildCurves(settingsMap, allNodes);
}
@Test
public void test_equalsHashCodeToString() {
CsvFile a1 = CsvFile.of(CharSource.wrap(CSV1), true);
CsvFile a2 = CsvFile.of(CharSource.wrap(CSV1), true);
CsvFile b = CsvFile.of(CharSource.wrap(CSV2), true);
CsvFile c = CsvFile.of(CharSource.wrap(CSV3), false);
// file
assertThat(a1.equals(a1)).isTrue();
assertThat(a1.equals(a2)).isTrue();
assertThat(a1.equals(b)).isFalse();
assertThat(a1.equals(c)).isFalse();
assertThat(a1.equals(null)).isFalse();
assertThat(a1.equals(ANOTHER_TYPE)).isFalse();
assertThat(a1.hashCode()).isEqualTo(a2.hashCode());
assertThat(a1.toString()).isNotNull();
// row
assertThat(a1.row(0).equals(a1.row(0))).isTrue();
assertThat(a1.row(0).equals(a2.row(0))).isTrue();
assertThat(a1.row(0).equals(b.row(0))).isFalse();
assertThat(c.row(0).equals(c.row(1))).isFalse();
assertThat(a1.row(0).equals(ANOTHER_TYPE)).isFalse();
assertThat(a1.row(0).equals(null)).isFalse();
assertThat(a1.row(0).hashCode()).isEqualTo(a2.row(0).hashCode());
assertThat(a1.row(0)).isNotNull();
}
@Test
public void test_parse_grid() {
CharSource source =
ResourceLocator.ofClasspath("com/opengamma/strata/loader/csv/sensitivity-grid.csv").getCharSource();
assertThat(LOADER.isKnownFormat(source)).isTrue();
ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER.parse(ImmutableList.of(source));
assertThat(test.getFailures().size()).as(test.getFailures().toString()).isEqualTo(0);
assertThat(test.getValue().size()).isEqualTo(1);
List<CurveSensitivities> list = test.getValue().get("");
assertThat(list).hasSize(1);
CurveSensitivities csens0 = list.get(0);
assertThat(csens0.getTypedSensitivities()).hasSize(1);
String tenors = "1D, 1W, 2W, 1M, 3M, 6M, 12M, 2Y, 5Y, 10Y";
assertSens(csens0, ZERO_RATE_DELTA, "GBP", GBP, tenors, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
assertSens(csens0, ZERO_RATE_DELTA, "USD-LIBOR-3M", USD, tenors, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
}
@Override
public CorpusEventLinking loadCorpusEventFrames(final CharSource source)
throws IOException {
int lineNo = 1;
try (final BufferedReader in = source.openBufferedStream()) {
final ImmutableSet.Builder<CorpusEventFrame> ret = ImmutableSet.builder();
String line;
while ((line = in.readLine()) != null) {
if (!line.isEmpty() && !line.startsWith("#")) {
// check for blank or comment lines
ret.add(parseLine(line));
}
}
return CorpusEventLinking.of(ret.build());
} catch (Exception e) {
throw new IOException("Error on line " + lineNo + " of " + source, e);
}
}
private static ImmutableMap<CurveGroupName, RatesCurveGroupDefinition> parse0(
CharSource groupsCharSource,
CharSource settingsCharSource,
Map<CurveName, SeasonalityDefinition> seasonality,
Collection<CharSource> curveNodeCharSources) {
// load curve groups and settings
List<RatesCurveGroupDefinition> curveGroups = RatesCurveGroupDefinitionCsvLoader.parseCurveGroupDefinitions(groupsCharSource);
Map<CurveName, LoadedCurveSettings> settingsMap = RatesCurvesCsvLoader.parseCurveSettings(settingsCharSource);
// load curve definitions
List<CurveDefinition> curveDefinitions = curveNodeCharSources.stream()
.flatMap(res -> parseSingle(res, settingsMap).stream())
.collect(toImmutableList());
// Add the curve definitions to the curve group definitions
return curveGroups.stream()
.map(groupDefinition -> groupDefinition.withCurveDefinitions(curveDefinitions).withSeasonalityDefinitions(seasonality))
.collect(toImmutableMap(groupDefinition -> groupDefinition.getName()));
}
@Test
public void test_parse_grid_tenorAndDateColumns() {
CharSource source = CharSource.wrap(
"Sensitivity Type,Sensitivity Tenor,Sensitivity Date,GBP\n" +
"ZeroRateGamma,1M,2018-06-30,1\n");
assertThat(LOADER.isKnownFormat(source)).isTrue();
ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER.parse(ImmutableList.of(source));
assertThat(test.getFailures().size()).as(test.getFailures().toString()).isEqualTo(0);
assertThat(test.getValue().size()).isEqualTo(1);
List<CurveSensitivities> list = test.getValue().get("");
assertThat(list).hasSize(1);
CurveSensitivities csens0 = list.get(0);
assertThat(csens0.getTypedSensitivities()).hasSize(1);
CurrencyParameterSensitivities cpss = csens0.getTypedSensitivity(ZERO_RATE_GAMMA);
assertThat(cpss.getSensitivities()).hasSize(1);
CurrencyParameterSensitivity cps = cpss.getSensitivities().get(0);
assertThat(cps.getParameterMetadata()).hasSize(1);
assertThat(cps.getParameterMetadata().get(0)).isEqualTo(TenorDateParameterMetadata.of(date(2018, 6, 30), TENOR_1M));
}
@Test
public void test_parse_grid_duplicateTenor() {
CharSource source = CharSource.wrap(
"Sensitivity Type,Sensitivity Tenor,GBP\n" +
"ZeroRateGamma,P6M,1\n" +
"ZeroRateGamma,12M,2\n" +
"ZeroRateGamma,12M,3\n");
assertThat(LOADER_DATE.isKnownFormat(source)).isTrue();
ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER_DATE.parse(ImmutableList.of(source));
assertThat(test.getFailures().size()).as(test.getFailures().toString()).isEqualTo(0);
assertThat(test.getValue().size()).isEqualTo(1);
List<CurveSensitivities> list = test.getValue().get("");
assertThat(list).hasSize(1);
CurveSensitivities csens0 = list.get(0);
assertThat(csens0.getTypedSensitivities()).hasSize(1);
assertSens(csens0, ZERO_RATE_GAMMA, "GBP", GBP, "6M, 1Y", 1, 5); // 12M -> 1Y
}
@Test
public void test_of_quoting() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV4), false);
assertThat(csvFile.rowCount()).isEqualTo(3);
assertThat(csvFile.row(0).fieldCount()).isEqualTo(2);
assertThat(csvFile.row(0).field(0)).isEqualTo("alpha");
assertThat(csvFile.row(0).field(1)).isEqualTo("be, \"at\", one");
assertThat(csvFile.row(1).fieldCount()).isEqualTo(3);
assertThat(csvFile.row(1).field(0)).isEqualTo("alpha\",be\"\"\"");
assertThat(csvFile.row(1).field(1)).isEqualTo("at\"\"");
assertThat(csvFile.row(1).field(2)).isEqualTo("one\"");
assertThat(csvFile.row(2).fieldCount()).isEqualTo(2);
assertThat(csvFile.row(2).field(0)).isEqualTo("r21");
assertThat(csvFile.row(2).field(1)).isEqualTo(" r22 ");
}
private List<String> readLines(HttpServletRequest req) throws IOException {
try (final InputStream inputStream = req.getInputStream()) {
CharSource charSource = new CharSource() {
@Override
public Reader openStream() throws IOException {
return new InputStreamReader(inputStream, Constants.UTF_8);
}
};
return charSource.readLines();
}
}
private List<String> readLines(HttpServletRequest req) throws IOException {
try (final InputStream inputStream = req.getInputStream()) {
CharSource charSource = new CharSource() {
@Override
public Reader openStream() throws IOException {
return new InputStreamReader(inputStream, Constants.UTF_8);
}
};
return charSource.readLines();
}
}
@Test
public void test_of_blank_row() {
try (CsvIterator csvFile = CsvIterator.of(CharSource.wrap(CSV3), false)) {
assertThat(csvFile.hasNext()).isTrue();
CsvRow row0 = csvFile.next();
assertThat(row0.fieldCount()).isEqualTo(2);
assertThat(row0.field(0)).isEqualTo("r11");
assertThat(row0.field(1)).isEqualTo("r12");
CsvRow row1 = csvFile.next();
assertThat(row1.fieldCount()).isEqualTo(2);
assertThat(row1.field(0)).isEqualTo("r21");
assertThat(row1.field(1)).isEqualTo("r22");
assertThat(csvFile.hasNext()).isFalse();
}
}
@Test
public void fromXML() throws IOException, XMLStreamException {
String testXml = "<gui><launcher><stuff-here-does-not-matter /></launcher></gui>";
XMLStreamReader xmlStream =
XMLInputFactory.newInstance()
.createXMLStreamReader(CharSource.wrap(testXml).openStream());
CfgGui unit = new CfgGui();
CfgGuiLauncher cfgGuiLauncher = mock(CfgGuiLauncher.class);
unit.setCfgGuiLauncher(cfgGuiLauncher);
unit.fromXML(xmlStream);
verify(cfgGuiLauncher).fromXML(xmlStream);
}
/**
* Format the given input (a Java compilation unit) into the output stream.
*
* @throws FormatterException if the input cannot be parsed
*/
public void formatSource(CharSource input, CharSink output)
throws FormatterException, IOException {
// TODO(cushon): proper support for streaming input/output. Input may
// not be feasible (parsing) but output should be easier.
output.write(formatSource(input.read()));
}
protected CharSource reader(final String resourceId) {
return new CharSource() {
@Override
public Reader openStream() throws IOException {
return new InputStreamReader(loader.getResource(resourceId).getInputStream(), UTF_8);
}
};
}
public static void scanWallets() {
File directory = getDefaultKeyDirectory();
keystoreMap.clear();
for (File file : directory.listFiles()) {
if (!file.getName().startsWith("identity")) {
try {
IMTKeystore keystore = null;
CharSource charSource = Files.asCharSource(file, Charset.forName("UTF-8"));
String jsonContent = charSource.read();
JSONObject jsonObject = new JSONObject(jsonContent);
int version = jsonObject.getInt("version");
if (version == 3) {
if (jsonContent.contains("encMnemonic")) {
keystore = unmarshalKeystore(jsonContent, V3MnemonicKeystore.class);
} else if (jsonObject.has("imTokenMeta") && ChainType.EOS.equals(jsonObject.getJSONObject("imTokenMeta").getString("chainType"))) {
keystore = unmarshalKeystore(jsonContent, LegacyEOSKeystore.class);
} else {
keystore = unmarshalKeystore(jsonContent, V3Keystore.class);
}
} else if (version == 1) {
keystore = unmarshalKeystore(jsonContent, V3Keystore.class);
} else if (version == 44) {
keystore = unmarshalKeystore(jsonContent, HDMnemonicKeystore.class);
} else if (version == 10001) {
keystore = unmarshalKeystore(jsonContent, EOSKeystore.class);
}
if (keystore != null) {
keystoreMap.put(keystore.getId(), keystore);
}
} catch (Exception ex) {
Log.e(LOG_TAG, "Can't loaded " + file.getName() + " file", ex);
}
}
}
}
@Test
public void test_standard_writeFile_customNewLine_noPadding() {
String fileContents = TWO_SECTIONS_NEWLINE_PADDED.replaceAll(" = ", "=");
IniFile file = IniFile.of(CharSource.wrap(fileContents));
StringBuilder buf = new StringBuilder();
IniFileOutput.standard(buf, false, "\n").writeIniFile(file);
assertThat(buf.toString()).isEqualTo(fileContents);
}
/**
* @param resourceName
* the classpath-relative location of the to-be-read resource
*/
private static List<String> getFileLines(final String resourceName) throws IOException {
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
}
};
CharSource charSrc = byteSource.asCharSource(Charsets.UTF_8);
return charSrc.readLines();
}
@Test
public void test_parse_list_allRowsBadNoEmptySensitvityCreated() {
CharSource source = CharSource.wrap(
"Reference,Sensitivity Tenor,ZeroRateDelta\n" +
"GBP,XX,1\n");
assertThat(LOADER_DATE.isKnownFormat(source)).isTrue();
ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER_DATE.parse(ImmutableList.of(source));
assertThat(test.getFailures()).hasSize(1);
assertThat(test.getValue().size()).isEqualTo(0);
FailureItem failure0 = test.getFailures().get(0);
assertThat(failure0.getReason()).isEqualTo(FailureReason.PARSING);
assertThat(failure0.getMessage())
.isEqualTo("CSV file could not be parsed at line 2: Invalid tenor 'XX', must be expressed as nD, nW, nM or nY");
}
@Override
public Writer openWriter() throws IOException {
final StringBuilder stringBuilder = new StringBuilder(DEFAULT_FILE_SIZE);
return new Writer() {
@Override
public void write(char[] chars, int start, int end) throws IOException {
stringBuilder.append(chars, start, end - start);
}
@Override
public void flush() throws IOException {}
@Override
public void close() throws IOException {
try {
formatter.formatSource(
CharSource.wrap(stringBuilder),
new CharSink() {
@Override
public Writer openStream() throws IOException {
return fileObject.openWriter();
}
});
} catch (FormatterException e) {
// An exception will happen when the code being formatted has an error. It's better to
// log the exception and emit unformatted code so the developer can view the code which
// caused a problem.
try (Writer writer = fileObject.openWriter()) {
writer.append(stringBuilder.toString());
}
if (messager != null) {
messager.printMessage(Diagnostic.Kind.NOTE, "Error formatting " + getName());
}
}
}
};
}
@Test
public void test_write_csv_iterator_always_quote() throws IOException {
CsvIterator iterator = CsvIterator.of(CharSource.wrap("a,b,c\n1,=2,3"), true);
try (StringWriter underlying = new StringWriter()) {
CsvOutput.standard(underlying, "\n", ",").writeCsvIterator(iterator, true);
assertThat(underlying.toString()).isEqualTo("\"a\",\"b\",\"c\"\n\"1\",\"=2\",\"3\"\n");
}
}
@Test
public void test_parse_standard_dateInTenorColumn() {
CharSource source = CharSource.wrap(
"Reference,Sensitivity Type,Sensitivity Tenor,Value\n" +
"GBP,ZeroRateGamma,2018-06-30,1\n");
assertThat(LOADER_DATE.isKnownFormat(source)).isTrue();
ValueWithFailures<ListMultimap<String, CurveSensitivities>> test = LOADER_DATE.parse(ImmutableList.of(source));
assertThat(test.getFailures()).hasSize(1);
assertThat(test.getValue().size()).isEqualTo(0);
FailureItem failure0 = test.getFailures().get(0);
assertThat(failure0.getReason()).isEqualTo(FailureReason.PARSING);
assertThat(failure0.getMessage())
.isEqualTo("CSV file could not be parsed at line 2: Invalid tenor '2018-06-30', must be expressed as nD, nW, nM or nY");
}
public static <R extends Readable & Closeable> Map<String, Distribution> loadDistribution(CharSource input)
throws IOException
{
Iterator<String> iterator = filter(input.readLines().iterator(), new Predicate<String>()
{
@Override
public boolean apply(String line)
{
line = line.trim();
return !line.isEmpty() && !line.startsWith("#");
}
});
return loadDistributions(iterator);
}
@Test
public void whenReadUsingCharSource_thenRead() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test1.in");
final CharSource source = Files.asCharSource(file, Charsets.UTF_8);
final String result = source.read();
assertEquals(expectedValue, result);
}
private boolean formatSourceFile(
File file, Formatter formatter, JavaFormatterOptions.Style style) {
if (file.isDirectory()) {
getLog().info("File '" + file + "' is a directory. Skipping.");
return true;
}
if (verbose) {
getLog().debug("Formatting '" + file + "'.");
}
CharSource source = com.google.common.io.Files.asCharSource(file, Charsets.UTF_8);
try {
String input = source.read();
String formatted = formatter.formatSource(input);
formatted = RemoveUnusedImports.removeUnusedImports(formatted);
if (!skipSortingImports) {
formatted = ImportOrderer.reorderImports(formatted, style);
}
if (!input.equals(formatted)) {
onNonComplyingFile(file, formatted);
nonComplyingFiles += 1;
}
filesProcessed.add(file.getAbsolutePath());
if (filesProcessed.size() % 100 == 0) {
logNumberOfFilesProcessed();
}
} catch (FormatterException | IOException e) {
getLog().error("Failed to format file '" + file + "'.", e);
return false;
}
return true;
}