/** * Annotate this item * * @param key the annotation key * @param value the value, or null to set a valueless annotation */ public void addAnnotation(String key, Object value) { if (annotations == null) annotations = new CopyOnWriteHashMap<>(); annotations.put(key, value); }
/** * Returns whether this has an annotation */ public boolean hasAnnotation(String annotation) { if (annotations == null) return false; return annotations.containsKey(annotation); }
private void makeWritable() { if (writable) return; map = copyMap(map); writable = true; entrySet = null; }
public Iterator<Map.Entry<Object,Integer>> getTokens() { return set.entrySet().iterator(); }
@Override public V put(K key, V value) { makeWritable(); return map.put(key, value); }
/** * Returns an annotation on this item, or null if the annotation is not set */ public Object getAnnotation(String annotation) { if (annotations == null) { return null; } return annotations.get(annotation); }
/** Returns a deep copy of this item */ public Item clone() { try { Item clone = (Item)super.clone(); if (this.annotations != null) clone.annotations = this.annotations.clone(); // note: connectedItem and connectedBacklink references are corrected in CompositeItem.clone() return clone; } catch (CloneNotSupportedException e) { throw new RuntimeException("Someone made Item unclonable"); } }
public Integer removeToken(String token) { return set.remove(token); }
@SuppressWarnings("unchecked") public CopyOnWriteHashMap<K,V> clone() { try { CopyOnWriteHashMap<K,V> clone = (CopyOnWriteHashMap<K,V>)super.clone(); this.makeReadOnly(); clone.makeReadOnly(); return clone; } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } }
@Override protected void appendBodyString(StringBuilder buffer) { buffer.append(indexName); buffer.append("{"); for (Map.Entry<Object, Integer> entry : set.entrySet()) { buffer.append("["); buffer.append(entry.getValue()); buffer.append("]:\""); buffer.append(entry.getKey()); buffer.append("\","); } buffer.deleteCharAt(buffer.length() - 1); // remove extra "," buffer.append("}"); }
/** Override to avoid using iterator.remove */ @Override public V remove(Object key) { makeWritable(); return map.remove(key); }
public Integer getTokenWeight(String token) { return set.get(token); }
@Override public WeightedSetItem clone() { WeightedSetItem clone = (WeightedSetItem)super.clone(); clone.set = this.set.clone(); return clone; }
@Override public void disclose(Discloser discloser) { super.disclose(discloser); discloser.addProperty("index", indexName); for (Map.Entry<Object, Integer> entry : set.entrySet()) { WordItem subitem = new WordItem(entry.getKey().toString(), indexName); subitem.setWeight(entry.getValue()); discloser.addChild(subitem); } }
@Override public int encode(ByteBuffer buffer) { encodeThis(buffer); int itemCount = 1; for (Map.Entry<Object, Integer> entry : set.entrySet()) { Object key = entry.getKey(); if (key instanceof Long) { new PureWeightedInteger((Long)key, entry.getValue()).encode(buffer); } else { new PureWeightedString(key.toString(), entry.getValue()).encode(buffer); } itemCount++; } return itemCount; }