/** * Creates a new format onlyWith the specified properties. The properties must be given as key * value pairs. */ public Format(Object... p) { this.properties = new HashMap<FormatKey<?>, Object>(); for (int i = 0; i < p.length; i += 2) { FormatKey<?> key = (FormatKey<?>) p[i]; if (!key.isAssignable(p[i + 1])) { throw new ClassCastException( key + ": " + p[i + 1] + " must be of type " + key.getValueClass()); } this.properties.put(key, p[i + 1]); } }
/** * Creates a new format which contains all properties from this format and additional properties * listed. * * <p>If a property is specified in both formats, then the property value from this format is * used. It overwrites that format. * * <p>If one of the format has more properties than the other, then the new format is more * specific than this format. * * @param p The properties must be given as key value pairs. * @return That format with properties overwritten by this format. */ public Format append(Object... p) { HashMap<FormatKey, Object> m = new HashMap<FormatKey, Object>(this.properties); for (int i = 0; i < p.length; i += 2) { FormatKey key = (FormatKey) p[i]; if (!key.isAssignable(p[i + 1])) { throw new ClassCastException( key + ": " + p[i + 1] + " must be of type " + key.getValueClass()); } m.put(key, p[i + 1]); } return new Format(m, false); }
/** * Creates a new format which contains all specified properties and additional properties from * this format. * * <p>If a property is specified in both formats, then the property value from this format is * used. It overwrites that format. * * <p>If one of the format has more properties than the other, then the new format is more * specific than this format. * * @param p The properties must be given as key value pairs. * @return That format with properties overwritten by this format. */ public Format prepend(Object... p) { HashMap<FormatKey<?>, Object> m = new HashMap<FormatKey<?>, Object>(); for (int i = 0; i < p.length; i += 2) { FormatKey<?> key = (FormatKey<?>) p[i]; if (!key.isAssignable(p[i + 1])) { throw new ClassCastException( key + ": " + p[i + 1] + " must be of type " + key.getValueClass()); } m.put(key, p[i + 1]); } for (Map.Entry<FormatKey<?>, Object> e : this.properties.entrySet()) { if (!m.containsKey(e.getKey())) { m.put(e.getKey(), e.getValue()); } } return new Format(m, false); }