1 /*
2 * Copyright 2011 FatWire Corporation. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.fatwire.gst.foundation.controller.action;
17
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.LinkedList;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Set;
26
27 /**
28 * This class is a container for Model data in the Model View Controller (MVC)
29 * framework.
30 *
31 * <p/>
32 * This class is not thread-safe.
33 *
34 * @author Dolf Dijkstra
35 * @since Apr 16, 2011
36 */
37 public class Model {
38
39 private final Map<String, Object> map = new HashMap<String, Object>();
40
41 /**
42 * Clears the model data.
43 */
44 public void reset() {
45 map.clear();
46 }
47
48 /**
49 * Removes the data with the name key.
50 *
51 * @param name
52 */
53 public void reset(final String name) {
54 map.remove(name);
55 }
56
57 /**
58 * Adds the key/value pair.
59 *
60 * @param key
61 * @param value
62 */
63 public void add(final String key, final Object value) {
64 map.put(key, value);
65 }
66
67 /**
68 * Adds the key/value pair, where are the values are added as a list.
69 *
70 * @param key
71 * @param value
72 */
73 public void add(final String key, final Object... value) {
74 map.put(key, Arrays.asList(value));
75 }
76
77 /**
78 * Adds the key/value pair, where are the value is added as a list.
79 *
80 * @param key
81 * @param value
82 */
83 @SuppressWarnings("unchecked")
84 public void list(final String key, final Object value) {
85 final Object v = map.get(key);
86 if (v instanceof Collection<?>) {
87 final Collection<Object> l = (Collection<Object>) v;
88 l.add(value);
89 } else {
90 final Collection<Object> l = new LinkedList<Object>();
91 if (v != null) {
92 l.add(v);
93 }
94 l.add(value);
95 map.put(key, l);
96 }
97 }
98
99 /**
100 * Returns all the key/value pairs.
101 *
102 * @return the entries as an unmodifiableSet, never null.
103 */
104 public Set<Entry<String, Object>> entries() {
105 return Collections.unmodifiableSet(map.entrySet());
106 }
107
108 /*
109 * (non-Javadoc)
110 *
111 * @see java.lang.Object#hashCode()
112 */
113 @Override
114 public int hashCode() {
115 final int prime = 31;
116 int result = 1;
117 result = prime * result + (map == null ? 0 : map.hashCode());
118 return result;
119 }
120
121 /*
122 * (non-Javadoc)
123 *
124 * @see java.lang.Object#equals(java.lang.Object)
125 */
126 @Override
127 public boolean equals(final Object obj) {
128 if (this == obj) {
129 return true;
130 }
131 if (obj == null) {
132 return false;
133 }
134 if (!(obj instanceof Model)) {
135 return false;
136 }
137 final Model other = (Model) obj;
138 if (map == null) {
139 if (other.map != null) {
140 return false;
141 }
142 } else if (!map.equals(other.map)) {
143 return false;
144 }
145 return true;
146 }
147
148 /*
149 * (non-Javadoc)
150 *
151 * @see java.lang.Object#toString()
152 */
153 @Override
154 public String toString() {
155 return "Model [map=" + map + "]";
156 }
157
158 }