All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prompto.intrinsic.PromptoDict Maven / Gradle / Ivy

The newest version!
package prompto.intrinsic;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;

@SuppressWarnings("serial")
public class PromptoDict extends HashMap implements Iterable>, IJsonNodeProducer {

	boolean mutable;

	public PromptoDict(boolean mutable) {
		this.mutable = mutable;
	}
	
	public boolean isMutable() {
		return mutable;
	}
	
	public void setMutable(boolean mutable) {
		this.mutable = mutable;
	}
	
	public PromptoDict swap() {
		PromptoDict swapped = new PromptoDict<>(true);
		for (HashMap.Entry kvp : entrySet()) {
			String key = String.valueOf(kvp.getValue());
			String value = String.valueOf(kvp.getKey());
			swapped.put(key, value);	
		}
		swapped.setMutable(false);
		return swapped;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("<");
		for (HashMap.Entry kvp : entrySet()) {
			String key = kvp.getKey().toString();
			if(!key.startsWith("\""))
				sb.append('"');
			sb.append(key);
			if(!key.endsWith("\""))
				sb.append('"');
			sb.append(":");
			sb.append(kvp.getValue().toString());
			sb.append(", ");
		}
		if (sb.length() > 2)
			sb.setLength(sb.length() - 2);
		else
			sb.append(":");
		sb.append(">");
		return sb.toString();
	}
	
	@Override
	public V get(Object key) {
		if(key==null)
			throw new NullPointerException();
		return super.get(key);
	}
	
	@Override
	public V put(K key, V value) {
		if(!mutable)
			PromptoException.throwEnumeratedException("NOT_MUTABLE");
		if(key==null)
			throw new NullPointerException();
		return super.put(key, value);
	}
	
	public Long getCount() {
		return (long)size();
	}
	
	public long getNativeCount() {
		return size();
	}
	
	public boolean contains(Object item) {
		return containsKey(item);
	}

	public boolean containsAll(Collection items) {
		return keySet().containsAll(items);
	}
	
	public boolean containsAny(Collection items) {
		for(Object item : items) {
			if(containsKey(item))
				return true;
		}
		return false;
	}

	public PromptoSet getKeys() {
		PromptoSet set = new PromptoSet();
		set.addAll(keySet()); // TODO worth the copy?
		return set;
	}
	
	public PromptoList getValues() {
		return new PromptoList(values(), false); // TODO worth the copy?
	}
	
	public static class Entry {

		HashMap.Entry entry;
		
		public Entry(HashMap.Entry entry) {
			this.entry = entry;
		}
		
		public K getKey() {
			return entry.getKey();
		}
		
		public V getValue() {
			return entry.getValue();
		}

	}

	@Override
	public Iterator> iterator() {
		return new Iterator>() {
			Iterator> iter = entrySet().iterator();
			@Override public boolean hasNext() { return iter.hasNext(); }
			@Override public Entry next() { return new PromptoDict.Entry<>(iter.next()); }
			
		};
	}

	public void removeValue(V value) {
		Set keys = entrySet().stream()
			.filter(e -> e.getValue().equals(value))
			.map(e -> e.getKey())
			.collect(Collectors.toSet());
		keys.forEach(this::remove);
	}
	
	@Override
	public JsonNode toJsonNode() {
		return new PromptoDocument(this).toJsonNode();
	}

}