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

com.logicbus.redis.util.BuilderFactory Maven / Gradle / Ivy

package com.logicbus.redis.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class BuilderFactory {
	public static final Builder DOUBLE = new Builder() {
		public Double build(Object data,Double t) {
			String asString = STRING.build(data,null);
			return asString == null ? null : Double.valueOf(asString);
		}

		public String toString() {
			return "double";
		}
	};
	public static final Builder BOOLEAN = new Builder() {
		public Boolean build(Object data,Boolean t) {
			return ((Long) data) == 1;
		}

		public String toString() {
			return "boolean";
		}
	};
	public static final Builder BYTE_ARRAY = new Builder() {
		public byte[] build(Object data,byte [] t) {
			return ((byte[]) data); // deleted == 1
		}

		public String toString() {
			return "byte[]";
		}
	};

	public static final Builder LONG = new Builder() {
		public Long build(Object data,Long t) {
			return (Long) data;
		}

		public String toString() {
			return "long";
		}

	};
	public static final Builder STRING = new Builder() {
		public String build(Object data,String t) {
			return data == null ? null : SafeEncoder.encode((byte[]) data);
		}

		public String toString() {
			return "string";
		}

	};
	public static final Builder> STRING_LIST = new Builder>() {
		@SuppressWarnings("unchecked")
		public List build(Object data,List t) {
			if (null == data) {
				return null;
			}
			List l = (List) data;
			final List result = (t == null) ? new ArrayList(l.size()) : t;
			for (final byte[] barray : l) {
				if (barray == null) {
					result.add(null);
				} else {
					result.add(SafeEncoder.encode(barray));
				}
			}
			return result;
		}

		public String toString() {
			return "List";
		}

	};
	
	public static final Builder> STRING_MAP = new Builder>() {
		@SuppressWarnings("unchecked")
		public Map build(Object data,Map t) {
			final List flatHash = (List) data;
			final Map hash = (t == null) ? new HashMap() : t;
			final Iterator iterator = flatHash.iterator();
			while (iterator.hasNext()) {
				hash.put(SafeEncoder.encode(iterator.next()),
						SafeEncoder.encode(iterator.next()));
			}

			return hash;
		}

		public String toString() {
			return "Map";
		}

	};
	
	public static final Builder> JSON_MAP = new Builder>() {
		@SuppressWarnings("unchecked")
		public Map build(Object data,Map t) {
			final List flatHash = (List) data;
			final Map hash = (t == null) ? new HashMap() : t;
			final Iterator iterator = flatHash.iterator();
			while (iterator.hasNext()) {
				hash.put(SafeEncoder.encode(iterator.next()),
						SafeEncoder.encode(iterator.next()));
			}

			return hash;
		}

		public String toString() {
			return "Map";
		}

	};	

	public static final Builder> STRING_SET = new Builder>() {
		@SuppressWarnings("unchecked")
		public Set build(Object data,Set t) {
			if (null == data) {
				return null;
			}
			List l = (List) data;
			final Set result = (t == null) ? new HashSet(l.size()) : t;
			for (final byte[] barray : l) {
				if (barray == null) {
					result.add(null);
				} else {
					result.add(SafeEncoder.encode(barray));
				}
			}
			return result;
		}

		public String toString() {
			return "Set";
		}

	};

	public static final Builder> BYTE_ARRAY_LIST = new Builder>() {
		@SuppressWarnings("unchecked")
		public List build(Object data,List t) {
			if (null == data) {
				return null;
			}
			List l = (List) data;

			return l;
		}

		public String toString() {
			return "List";
		}
	};

	public static final Builder> BYTE_ARRAY_ZSET = new Builder>() {
		@SuppressWarnings("unchecked")
		public Set build(Object data,Set t) {
			if (null == data) {
				return null;
			}
			List l = (List) data;
			final Set result = (t == null)?new LinkedHashSet(l):t;
			for (final byte[] barray : l) {
				if (barray == null) {
					result.add(null);
				} else {
					result.add(barray);
				}
			}
			return result;
		}

		public String toString() {
			return "ZSet";
		}
	};
	public static final Builder> BYTE_ARRAY_MAP = new Builder>() {
		@SuppressWarnings("unchecked")
		public Map build(Object data,Map t) {
			final List flatHash = (List) data;
			final Map hash = (t == null)?new HashMap():t;
			final Iterator iterator = flatHash.iterator();
			while (iterator.hasNext()) {
				hash.put(iterator.next(), iterator.next());
			}

			return hash;
		}

		public String toString() {
			return "Map";
		}

	};

	public static final Builder> STRING_ZSET = new Builder>() {
		@SuppressWarnings("unchecked")
		public Set build(Object data,Set t) {
			if (null == data) {
				return null;
			}
			List l = (List) data;
			final Set result = (t == null)? new LinkedHashSet(l.size()) :t;
			for (final byte[] barray : l) {
				if (barray == null) {
					result.add(null);
				} else {
					result.add(SafeEncoder.encode(barray));
				}
			}
			return result;
		}

		public String toString() {
			return "ZSet";
		}

	};
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy