Java tutorial
/* * Rubric Identity Manager * Copyright (C) 2013, Metalware Software * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.metalware.rubric.core.dto; import org.joda.time.DateTime; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * Defines a builder style class that encapsulates a map object. * The internal map object can either be a new map, or it can point * to a map that was created by MongoDB (in the form of a BasicDBObject). * This goal of this class is to define utility methods that make * working with maps more easier than just using the map itself. The * mutate methods are self-referencing for chaining across attribute * assignments. * @since 1.0 * @version 1.0 * @author Dan Ingalla */ public final class Document { /** * Defines the common fields that are used by * most Document types. Not all documents will contain * this data, especially if they are children of other * documents. */ public enum CommonFields { /** The unique identifier of the document */ _id, /** Specifies the account that created the document */ createdBy, /** Specifies the date when the document was created */ createdDate, /** Specifies the account that modified the document */ modifiedBy, /** Specifies the date when the document was modified */ modifiedDate } // The encapsulated structure final Map<String, Object> map; // A private constructor-force tuse of he static create methods. private Document(Map<String, Object> source) { this.map = source; } /** * Creates a new instance of this class type * using the default capacity and load factor. * @return A new instance. */ public static Document create() { return new Document(new LinkedHashMap<String, Object>()); } /** * Creates a new instance of this class type * using the default load factor and the specified * capacity. * @param capacity The initial number of key/value pair * nodes to allocate within the map. * @return A new instance. */ public static Document create(int capacity) { return new Document(new LinkedHashMap<String, Object>(capacity)); } /** * Creates a new instance of this class type. The given * source map is copied instead of being referenced. Use this * option when you do not wish to modify the contents of the * original map. * @param source The source map to copy. * @return A new instance. */ public static Document copy(Map<String, Object> source) { return new Document(new LinkedHashMap<String, Object>(source)); } /** * Creates a new instance of this class type. The * internal map is assigned the source map given. * @param source The map to wrap. * @return A Document instance. */ public static Document wrap(Map<String, Object> source) { return new Document(source); } // Accessor functions /** * Returns a reference to the map that is encapsulated by * this document. * @return A Map instance. */ public Map<String, Object> map() { return this.map; } /** * Gets a boolean value from the internal map. The given * key is first converted to a string before retrieval. * @param key The key of the value to retrieve. * @return The map value, or the default value for the * type if the key isn't located. */ public boolean getBoolean(Object key) { return (Boolean) map.get(key.toString()); } /** * Gets a integer value from the internal map. The given * key is first converted to a string before retrieval. * @param key The key of the value to retrieve. * @return The map value, or the default value for the * type if the key isn't located. */ public int getInteger(Object key) { return (Integer) map.get(key.toString()); } /** * Gets a long integer value from the internal map. The given * key is first converted to a string before retrieval. * @param key The key of the value to retrieve. * @return The map value, or the default value for the * type if the key isn't located. */ public long getLong(Object key) { return (Long) map.get(key.toString()); } /** * Gets a date value from the internal map. The given * key is first converted to a string before retrieval. * @param key The key of the value to retrieve. * @return The map value, or the default value for the * type if the key isn't located. */ public DateTime getDateTime(Object key) { return map.containsKey(key.toString()) ? new DateTime(getLong(key)) : null; } /** * Gets a list from the internal map. The given key is * first converted to a string before retrieval. * @param key The key of the value to retrieve. * @param <T> The list element type. * @return A reference to a list, or null if the key * could not be found. */ public <T> List<T> getList(Object key) { return (List<T>) map.get(key.toString()); } /** * Gets a list copy from the internal map. The given key is * first converted to a string before retrieval. * @param key The key of the value to retrieve. * @param <T> The list element type. * @return A reference to a list, or null if the key * could not be found. */ public <T> List<T> getListCopy(Object key) { final List<T> source = getList(key); return source != null ? new ArrayList<T>(source) : null; } /** * Gets a sub map from the internal map. The given key is * first converted to a string before retrieval. * @param key The key of the value to retrieve. * @return A reference to a map, or null if the key * could not be found. */ public Map<String, Object> getMap(Object key) { return (Map<String, Object>) map.get(key.toString()); } /** * Gets a sub map from the internal map. The given key is * first converted to a string before retrieval. * @param key The key of the value to retrieve. * @return A reference to a map, or null if the key * could not be found. */ public Map<String, Object> getMapCopy(Object key) { final Map<String, Object> source = getMap(key); return source != null ? new LinkedHashMap<String, Object>(source) : null; } /** * Gets a string value from the internal map. The given * key is first converted to a string before retrieval. * @param key The key of the value to retrieve. * @return The map value, or the default value for the * type if the key isn't located. */ public String getString(Object key) { return (String) map.get(key.toString()); } /** * Gets a value from the internal map. The given * key is first converted to a string before retrieval. * @param key The key of the value to retrieve. * @return The map value, or the default value for the * type if the key isn't located. */ public Object get(Object key) { return map.get(key.toString()); } // Mutate functions /** * Removes all values from the document. * @return A reference to this instance. */ public Document clear() { map.clear(); return this; } /** * Removes a value from the document. * @param key The key to remove. * @return A reference to this instance. */ public Document remove(Object key) { map.remove(key.toString()); return this; } /** * Sets a value within the document. * @param key The key of the value to set. The argument is converted to a string. * @param value The value to assign. * @return A reference to this instance. */ public Document setBoolean(Object key, boolean value) { map.put(key.toString(), value); return this; } /** * Sets a value within the document. * @param key The key of the value to set. The argument is converted to a string. * @param value The value to assign. * @return A reference to this instance. */ public Document setInteger(Object key, int value) { map.put(key.toString(), value); return this; } /** * Sets a value within the document. * @param key The key of the value to set. The argument is converted to a string. * @param value The value to assign. * @return A reference to this instance. */ public Document setLong(Object key, long value) { map.put(key.toString(), value); return this; } /** * Sets a value within the document. * @param key The key of the value to set. The argument is converted to a string. * @param value The value to assign. * @return A reference to this instance. */ public Document setDateTime(Object key, DateTime value) { setLong(key, value.getMillis()); return this; } /** * Sets a value within the document. * @param key The key of the value to set. The argument is converted to a string. * @param value The value to assign. * @return A reference to this instance. */ public Document setList(Object key, List value) { map.put(key.toString(), value); return this; } /** * Sets a value within the document. * @param key The key of the value to set. The argument is converted to a string. * @param value The value to assign. * @return A reference to this instance. */ public Document setMap(Object key, Map<String, Object> value) { map.put(key.toString(), value); return this; } /** * Sets a value within the document. * @param key The key of the value to set. The argument is converted to a string. * @param value The value to assign. * @return A reference to this instance. */ public Document setString(Object key, String value) { map.put(key.toString(), value); return this; } }