public void putAll(CompositeValue[] values) { if (values == null) return; HashSet<List<MetaValue>> keys = new HashSet<List<MetaValue>>(); for (int i = 0; i < values.length; ++i) { List<MetaValue> index = Arrays.asList(calculateIndex(values[i])); if (keys.contains(index)) throw new IllegalArgumentException("Duplicate index in values " + index + " for value " + values[i]); keys.add(index); if (dataMap.containsKey(index)) throw new IllegalArgumentException("Index already used " + index + " for value " + values[i]); } for (int i = 0; i < values.length; i++) put(values[i]); }
/** * Construct Table value * * @param tableType the table type * @param initialCapacity the initial capacity of the map * @param loadFactor the load factory of the map * @exception IllegalArgumentException for a null argument */ public TableValueSupport(TableMetaType tableType, int initialCapacity, float loadFactor) { init(new HashMap<List<MetaValue>, CompositeValue>(initialCapacity, loadFactor), tableType); }
public MetaValue[] calculateIndex(CompositeValue value) { validateCompositeValue(value); return value.getAll(indexNames); }
/** * Create a table value * * @param type the type * @param value the value * @param mapping the mapping * @return the table value */ @SuppressWarnings("unchecked") public TableValue createTableValue(TableMetaType type, Map value, Map<Object, MetaValue> mapping) { if (value == null) return null; TableValueSupport table = new TableValueSupport(type); mapping.put(value, table); CompositeMetaType entryType = type.getRowType(); MetaType keyType = entryType.getType(DefaultMetaTypeFactory.MAP_KEY); MetaType valType = entryType.getType(DefaultMetaTypeFactory.MAP_VALUE); for (Iterator<Map.Entry> i = value.entrySet().iterator(); i.hasNext();) { Map.Entry entry = i.next(); MetaValue key = internalCreate(entry.getKey(), null, keyType); MetaValue val = internalCreate(entry.getValue(), null, valType); CompositeValueSupport data = new CompositeValueSupport(entryType, DefaultMetaTypeFactory.MAP_ITEM_NAMES, new MetaValue[] { key, val }); table.put(data); } return table; }
public CompositeValue get(MetaValue[] key) { validateKey(key); return dataMap.get(Arrays.asList(key)); }
public void put(CompositeValue value) { List<MetaValue> index = Arrays.asList(calculateIndex(value)); if (dataMap.containsKey(index)) throw new IllegalArgumentException("The index is already used " + index); dataMap.put(index, value); }
@Override public String toString() { StringBuilder buffer = new StringBuilder(getClass().getSimpleName()); buffer.append(": tableType=["); buffer.append(getMetaType()); buffer.append("] mappings=["); Iterator<Entry<List<MetaValue>, CompositeValue>> entries = dataMap.entrySet().iterator(); while(entries.hasNext()) { Entry<List<MetaValue>, CompositeValue> entry = entries.next(); buffer.append(entry.getKey()); buffer.append("="); buffer.append(entry.getValue()); if (entries.hasNext()) buffer.append(","); } buffer.append("]"); return buffer.toString(); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || (obj instanceof TableValue) == false) return false; TableValue other = (TableValue) obj; if (tableType.equals(other.getMetaType()) == false) return false; if (size() != other.size()) return false; for (Entry<List<MetaValue>, CompositeValue> entry : dataMap.entrySet()) { List<MetaValue> list = entry.getKey(); MetaValue[] indexes = list.toArray(new MetaValue[list.size()]); CompositeValue thisValue = entry.getValue(); CompositeValue otherValue = other.get(indexes); if (thisValue == null && otherValue == null) return true; if (thisValue == null && otherValue != null) return false; if (thisValue.equals(otherValue) == false) return false; } return true; }
/** * Create a table value * * @param type the type * @param value the value * @param mapping the mapping * @return the table value */ @SuppressWarnings("unchecked") public TableValue createTableValue(TableMetaType type, Map value, Map<Object, MetaValue> mapping) { if (value == null) return null; TableValueSupport table = new TableValueSupport(type); mapping.put(value, table); CompositeMetaType entryType = type.getRowType(); MetaType keyType = entryType.getType(DefaultMetaTypeFactory.MAP_KEY); MetaType valType = entryType.getType(DefaultMetaTypeFactory.MAP_VALUE); for (Iterator<Map.Entry> i = value.entrySet().iterator(); i.hasNext();) { Map.Entry entry = i.next(); MetaValue key = internalCreate(entry.getKey(), null, keyType); MetaValue val = internalCreate(entry.getValue(), null, valType); CompositeValueSupport data = new CompositeValueSupport(entryType, DefaultMetaTypeFactory.MAP_ITEM_NAMES, new MetaValue[] { key, val }); table.put(data); } return table; }
public CompositeValue remove(MetaValue[] key) { validateKey(key); return dataMap.remove(Arrays.asList(key)); }
public void put(CompositeValue value) { List<MetaValue> index = Arrays.asList(calculateIndex(value)); if (dataMap.containsKey(index)) throw new IllegalArgumentException("The index is already used " + index); dataMap.put(index, value); }
@Override public String toString() { StringBuilder buffer = new StringBuilder(getClass().getSimpleName()); buffer.append(": tableType=["); buffer.append(getMetaType()); buffer.append("] mappings=["); Iterator<Entry<List<MetaValue>, CompositeValue>> entries = dataMap.entrySet().iterator(); while(entries.hasNext()) { Entry entry = entries.next(); buffer.append(entry.getKey()); buffer.append("="); buffer.append(entry.getValue()); if (entries.hasNext()) buffer.append(","); } buffer.append("]"); return buffer.toString(); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || (obj instanceof TableValue) == false) return false; TableValue other = (TableValue) obj; if (tableType.equals(other.getMetaType()) == false) return false; if (size() != other.size()) return false; for (Entry<List<MetaValue>, CompositeValue> entry : dataMap.entrySet()) { List<MetaValue> list = entry.getKey(); MetaValue[] indexes = list.toArray(new MetaValue[list.size()]); CompositeValue thisValue = entry.getValue(); CompositeValue otherValue = other.get(indexes); if (thisValue == null && otherValue == null) return true; if (thisValue == null && otherValue != null) return false; if (thisValue.equals(otherValue) == false) return false; } return true; }
/** * Create the table value. * * @param table the persisted table value * @param type the table meta type * @return the table value */ protected TableValue createTableValue(PersistedTableValue table, TableMetaType type) { TableValueSupport support = new TableValueSupport(type); if(table.getEntries() != null && table.getEntries().isEmpty() == false) { for(PersistedCompositeValue entry : table.getEntries()) { support.put(createCompositeValue(entry, type.getRowType())); } } return support; }
public void putAll(CompositeValue[] values) { if (values == null) return; HashSet<List<MetaValue>> keys = new HashSet<List<MetaValue>>(); for (int i = 0; i < values.length; ++i) { List<MetaValue> index = Arrays.asList(calculateIndex(values[i])); if (keys.contains(index)) throw new IllegalArgumentException("Duplicate index in values " + index + " for value " + values[i]); keys.add(index); if (dataMap.containsKey(index)) throw new IllegalArgumentException("Index already used " + index + " for value " + values[i]); } for (int i = 0; i < values.length; i++) put(values[i]); }
/** * Construct Table value * * @param tableType the table type * @param initialCapacity the initial capacity of the map * @param loadFactor the load factory of the map * @exception IllegalArgumentException for a null argument */ public TableValueSupport(TableMetaType tableType, int initialCapacity, float loadFactor) { init(new HashMap<List<MetaValue>, CompositeValue>(initialCapacity, loadFactor), tableType); }
public CompositeValue get(MetaValue[] key) { validateKey(key); return dataMap.get(Arrays.asList(key)); }
public MetaValue[] calculateIndex(CompositeValue value) { validateCompositeValue(value); return value.getAll(indexNames); }
@SuppressWarnings("unchecked") private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { ObjectInputStream.GetField getField = in.readFields(); HashMap<List<MetaValue>, CompositeValue> dataMap = (HashMap<List<MetaValue>, CompositeValue>) getField.get("dataMap", null); TableMetaType tableType = (TableMetaType) getField.get("tableType", null); try { init(dataMap, tableType); } catch (Exception e) { throw new RuntimeException("Unexpected error during deserialization", e); } } }
public CompositeValue remove(MetaValue[] key) { validateKey(key); return dataMap.remove(Arrays.asList(key)); }