@Override public synchronized V getOrDefault(Object key, V defaultValue) { return getDelegate().getOrDefault(key, defaultValue); }
@Override public synchronized V getOrDefault(Object key, V defaultValue) { return getDelegate().getOrDefault(key, defaultValue); }
public class ApplicationSettings { private final Properties properties = new Properties(); public ApplicationSettings() { properties.load(...); } public String emailFrom() { // simple methods are concise, and encode the default right inline return properties.getOrDefault("emailFrom", "hello@abc.com"); } public int getMaxTestSeconds() { // You can do more complex validation if you want, too String value = properties.get("maxTestInSec"); if (value == null) { return 30; } int maxTestSeconds = Integer.parseInt(value); if (maxTestSeconds <= 0) { // could instead log a warning and return the default if you want throw new IllegalStateException( "maxTestInSec must be positive - was " + maxTestSeconds); } return maxTestSeconds; } }
/** * Get result. */ @Override public void getResult(final ContentHandler buf) throws SAXException { final String separator = paramTable.getOrDefault("separator", "|"); final List<String> v = valueSet.stream() .map(fileValue -> fileValue.value) .collect(Collectors.toList()); Collections.sort(v); final StringBuilder retBuf = new StringBuilder(); for (final Iterator<String> i = v.iterator(); i.hasNext();) { retBuf.append(i.next()); if (i.hasNext()) { retBuf.append(separator); } } final char[] ret = retBuf.toString().toCharArray(); buf.characters(ret, 0, ret.length); }
Optional<Object> cast(Class<?> c, String s) { try { // try scalar types return Optional.of(Type.of(c).parse(s)); } catch (UnsupportedOperationException uoe) { return Optional.ofNullable(classMappers.getOrDefault(c, k -> { try { return c.getConstructor(String.class).newInstance(s); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { return doThrow(e); } catch (NoSuchMethodException | SecurityException e) { return null; } }).apply(s)); } }
/** * Get result. */ @Override public void getResult(final ContentHandler buf) throws SAXException { final String property = paramTable.getOrDefault("property", "transtype"); for (final Value value: valueSet) { buf.startElement(NULL_NS_URI, "not", "not", new AttributesBuilder().build()); buf.startElement(NULL_NS_URI, "equals", "equals", new AttributesBuilder() .add("arg1", "${" + property + "}") .add("arg2", value.value) .add("casesensitive", "false") .build()); buf.endElement(NULL_NS_URI, "equals", "equals"); buf.endElement(NULL_NS_URI, "not", "not"); } }
@Test(expected = ReactiveAuditException.class) public void getOrDefault() { ReactiveAudit.off.commit(); Hashtable hash=new Hashtable(); TestTools.strict.commit(); hash.getOrDefault(null,null); } @Test(expected = ReactiveAuditException.class)