/** * Returns the property set as a map, throwing an exception if any key has multiple values. * <p> * The iteration order of the map matches that of the input data. * * @return the key-value map */ public ImmutableMap<String, String> asMap() { ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); for (String key : keys()) { builder.put(key, value(key)); } return builder.build(); }
public void test_empty() { PropertySet test = PropertySet.empty(); assertEquals(test.isEmpty(), true); assertEquals(test.contains("unknown"), false); assertEquals(test.valueList("unknown"), ImmutableList.of()); assertThrowsIllegalArg(() -> test.value("unknown")); assertEquals(test.toString(), "{}"); }
private static ImmutableMap<String, String> parseAlternates(IniFile config) { if (!config.contains(ALTERNATES_SECTION)) { return ImmutableMap.of(); } Map<String, String> alternates = new HashMap<>(); for (Entry<String, String> entry : config.section(ALTERNATES_SECTION).asMap().entrySet()) { alternates.put(entry.getKey(), entry.getValue()); alternates.putIfAbsent(entry.getKey().toUpperCase(Locale.ENGLISH), entry.getValue()); } return ImmutableMap.copyOf(alternates); }
private TradeReportColumn parseColumn(String columnName, PropertySet properties) { TradeReportColumn.Builder columnBuilder = TradeReportColumn.builder(); columnBuilder.header(columnName); if (properties.contains(VALUE_PROPERTY)) { columnBuilder.value(properties.value(VALUE_PROPERTY)); } if (properties.contains(IGNORE_FAILURES_PROPERTY)) { String ignoreFailuresValue = properties.value(IGNORE_FAILURES_PROPERTY); boolean ignoresFailure = Boolean.valueOf(ignoreFailuresValue); columnBuilder.ignoreFailures(ignoresFailure); } return columnBuilder.build(); }
public void test_equalsHashCode() { Map<String, String> keyValues = ImmutableMap.of("a", "x", "b", "y"); PropertySet a1 = PropertySet.of(keyValues); PropertySet a2 = PropertySet.of(keyValues); PropertySet b = PropertySet.of(ImmutableMap.of("a", "x", "b", "z")); assertEquals(a1.equals(a1), true); assertEquals(a1.equals(a2), true); assertEquals(a1.equals(b), false); assertEquals(a1.equals(null), false); assertEquals(a1.equals(ANOTHER_TYPE), false); assertEquals(a1.hashCode(), a2.hashCode()); }
public void test_of_map() { Map<String, String> keyValues = ImmutableMap.of("a", "x", "b", "y"); PropertySet test = PropertySet.of(keyValues); assertEquals(test.isEmpty(), false); assertEquals(test.contains("a"), true); assertEquals(test.value("a"), "x"); assertEquals(test.valueList("a"), ImmutableList.of("x")); assertEquals(test.contains("b"), true); assertEquals(test.value("b"), "y"); assertEquals(test.valueList("b"), ImmutableList.of("y")); assertEquals(test.contains("c"), false); assertEquals(test.keys(), ImmutableSet.of("a", "b")); assertEquals(test.asMap(), ImmutableMap.of("a", "x", "b", "y")); assertEquals(test.asMultimap(), ImmutableListMultimap.of("a", "x", "b", "y")); assertEquals(test.valueList("unknown"), ImmutableSet.of()); assertThrowsIllegalArg(() -> test.value("unknown")); assertEquals(test.toString(), "{a=[x], b=[y]}"); }
public void test_of_propertyNoEquals() { IniFile test = IniFile.of(CharSource.wrap("[section]\na\n")); Multimap<String, String> keyValues1 = ImmutableListMultimap.of("a", ""); assertEquals(test.asMap(), ImmutableMap.of("section", PropertySet.of(keyValues1))); assertEquals(test.section("section"), PropertySet.of(keyValues1)); assertEquals(test.section("section").contains("a"), true); assertEquals(test.section("section").valueList("a"), ImmutableList.of("")); assertEquals(test.section("section").contains("b"), false); assertEquals(test.section("section").keys(), ImmutableSet.of("a")); assertEquals(test.section("section").asMultimap(), ImmutableListMultimap.of("a", "")); assertEquals(test.toString(), "{section={a=[]}}"); }
/** * Combines this property set with another. * <p> * This property set takes precedence. * Any order of any additional keys will be retained, with those keys located after the base set of keys. * * @param other the other property set * @return the combined property set */ public PropertySet combinedWith(PropertySet other) { ArgChecker.notNull(other, "other"); if (other.isEmpty()) { return this; } if (isEmpty()) { return other; } // cannot use ArrayListMultiMap as it does not retain the order of the keys // whereas ImmutableListMultimap does retain the order of the keys ImmutableListMultimap.Builder<String, String> map = ImmutableListMultimap.builder(); map.putAll(this.keyValueMap); for (String key : other.keyValueMap.keySet()) { if (!this.contains(key)) { map.putAll(key, other.valueList(key)); } } return new PropertySet(map.build()); }
private static PropertySet parse(ImmutableList<String> lines) { // cannot use ArrayListMultiMap as it does not retain the order of the keys // whereas ImmutableListMultimap does retain the order of the keys ImmutableListMultimap.Builder<String, String> parsed = ImmutableListMultimap.builder(); int lineNum = 0; for (String line : lines) { lineNum++; line = line.trim(); if (line.length() == 0 || line.startsWith("#") || line.startsWith(";")) { continue; } int equalsPosition = line.indexOf(" = "); equalsPosition = equalsPosition < 0 ? line.indexOf('=') : equalsPosition + 1; String key = (equalsPosition < 0 ? line.trim() : line.substring(0, equalsPosition).trim()); String value = (equalsPosition < 0 ? "" : line.substring(equalsPosition + 1).trim()); if (key.length() == 0) { throw new IllegalArgumentException("Invalid properties file, empty key, line " + lineNum); } parsed.put(key, value); } return PropertySet.of(parsed.build()); }
private static ImmutableMap<CurrencyPair, Integer> parsePairs(IniFile ini) { ImmutableMap.Builder<CurrencyPair, Integer> builder = ImmutableMap.builder(); for (Entry<String, PropertySet> entry : ini.asMap().entrySet()) { String pairStr = entry.getKey(); if (CurrencyPair.REGEX_FORMAT.matcher(pairStr).matches()) { CurrencyPair pair = CurrencyPair.parse(pairStr); PropertySet properties = entry.getValue(); Integer rateDigits = Integer.parseInt(properties.value("rateDigits")); builder.put(pair, rateDigits); } } return builder.build(); }
public void test_combinedWith_emptyBase() { PropertySet base = PropertySet.of(ImmutableListMultimap.of("a", "x", "a", "y", "b", "y", "c", "z")); assertEquals(base.combinedWith(PropertySet.empty()), base); }
public void test_overrideWith_emptyBase() { PropertySet base = PropertySet.of(ImmutableListMultimap.of("a", "x", "a", "y", "b", "y", "c", "z")); assertEquals(base.overrideWith(PropertySet.empty()), base); }
public void test_equalsHashCode_section() { IniFile a1 = IniFile.of(CharSource.wrap(INI1)); IniFile a2 = IniFile.of(CharSource.wrap(INI1)); IniFile b = IniFile.of(CharSource.wrap(INI2)); assertEquals(a1.section("name").equals(a1.section("name")), true); assertEquals(a1.section("name").equals(a2.section("name")), true); assertEquals(a1.section("name").equals(b.section("section")), false); assertEquals(a1.section("name").equals(null), false); assertEquals(a1.section("name").equals(ANOTHER_TYPE), false); assertEquals(a1.section("name").hashCode(), a2.section("name").hashCode()); }
public void test_overrideWith() { PropertySet base = PropertySet.of(ImmutableListMultimap.of("a", "x", "a", "y", "b", "y", "c", "z")); PropertySet other = PropertySet.of(ImmutableListMultimap.of("a", "aa", "c", "cc", "d", "dd", "e", "ee")); PropertySet expected = PropertySet.of(ImmutableListMultimap.of("a", "aa", "b", "y", "c", "cc", "d", "dd", "e", "ee")); assertEquals(base.overrideWith(other), expected); }
public void test_combinedWith() { PropertySet base = PropertySet.of(ImmutableListMultimap.of("a", "x", "a", "y", "c", "z")); PropertySet other = PropertySet.of(ImmutableListMultimap.of("a", "aa", "b", "bb", "d", "dd")); PropertySet expected = PropertySet.of(ImmutableListMultimap.of("a", "x", "a", "y", "c", "z", "b", "bb", "d", "dd")); assertEquals(base.combinedWith(other), expected); }
for (String key : section.keys()) { Class<?> cls; try {
/** * Returns a suitable hash code for the file. * * @return the hash code */ @Override public int hashCode() { return keyValueMap.hashCode(); }
/** * Returns a string describing the file. * * @return the descriptive string */ @Override public String toString() { return keyValueMap.toString(); }
/** * Checks if this file equals another. * <p> * The comparison checks the content. * * @param obj the other section, null returns false * @return true if equal */ @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof PropertiesFile) { return keyValueMap.equals(((PropertiesFile) obj).keyValueMap); } return false; }