|
| 1 | +/* |
| 2 | + * Copyright (c) 2012, 2013, Werner Keil, Credit Suisse (Anatole Tresch). |
| 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 | + * Contributors: |
| 17 | + * Anatole Tresch - initial version. |
| 18 | + */ |
| 19 | +package org.javamoney.extras; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +/** |
| 25 | + * Defines a {@link MultiValue} containing T instances. |
| 26 | + * |
| 27 | + * @see CompoundValue |
| 28 | + * @author Anatole Tresch |
| 29 | + */ |
| 30 | +public class MultiValue extends HashMap<String, Object> implements |
| 31 | + CompoundValue { |
| 32 | + |
| 33 | + /** |
| 34 | + * serialVersionUID. |
| 35 | + */ |
| 36 | + private static final long serialVersionUID = 5511830637352838197L; |
| 37 | + private final String id; |
| 38 | + private CompoundType type; |
| 39 | + |
| 40 | + protected MultiValue(String id, CompoundType type) { |
| 41 | + if (id == null) { |
| 42 | + throw new IllegalArgumentException("id can not bve null."); |
| 43 | + } |
| 44 | + this.id = id; |
| 45 | + if (type == null) { |
| 46 | + throw new IllegalArgumentException("type can not bve null."); |
| 47 | + } |
| 48 | + this.type = type; |
| 49 | + } |
| 50 | + |
| 51 | + protected MultiValue(String id, CompoundType type, Map<String, Object> items) { |
| 52 | + this(id, type); |
| 53 | + type.validate(items); |
| 54 | + this.putAll(items); |
| 55 | + } |
| 56 | + |
| 57 | + public CompoundType getCompoundType() { |
| 58 | + return this.type; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * A compound item may have a type identifier that helps to identify, what |
| 63 | + * type of compound items object is returned. |
| 64 | + * |
| 65 | + * @return the compound item's type, never null. |
| 66 | + */ |
| 67 | + public String getId() { |
| 68 | + return this.id; |
| 69 | + } |
| 70 | + |
| 71 | + public Builder toBuilder() { |
| 72 | + return new Builder(this); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * A CompoundItemBuilder T is an CompoundItemBuilder that holds several |
| 77 | + * instances of a type T. In financial applications this is very useful |
| 78 | + * regarding several aspects:<br/> |
| 79 | + * <li>Passing several instance of T as one parameter into methods <li> |
| 80 | + * Returning several instance of T as result, e.g. from a complex financial |
| 81 | + * calculation. <li>Accessing series of data, e.g. by time or other |
| 82 | + * classification. <li> |
| 83 | + * Accessing multiple data from different providers. |
| 84 | + * |
| 85 | + * |
| 86 | + * @author Anatole Tresch |
| 87 | + */ |
| 88 | + public static class Builder { |
| 89 | + |
| 90 | + private String id; |
| 91 | + private CompoundType type; |
| 92 | + private Map<String, Object> items = new HashMap<String, Object>(); |
| 93 | + |
| 94 | + public Builder(String id) { |
| 95 | + setId(id); |
| 96 | + } |
| 97 | + |
| 98 | + public Builder(String id, CompoundType type) { |
| 99 | + setId(id); |
| 100 | + setCompoundType(type); |
| 101 | + } |
| 102 | + |
| 103 | + public Builder setCompoundType(CompoundType type) { |
| 104 | + if (type == null) { |
| 105 | + throw new IllegalArgumentException("Compound type required."); |
| 106 | + } |
| 107 | + this.type = type; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public CompoundType getCompoundType() { |
| 112 | + return this.type; |
| 113 | + } |
| 114 | + |
| 115 | + public Builder(CompoundValue baseItem) { |
| 116 | + if (baseItem != null) { |
| 117 | + setId(baseItem.getId()); |
| 118 | + this.items.putAll(baseItem); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public String getId() { |
| 123 | + return this.id; |
| 124 | + } |
| 125 | + |
| 126 | + public Builder setId(String id) { |
| 127 | + if (id == null) { |
| 128 | + throw new IllegalArgumentException("id may not be null."); |
| 129 | + } |
| 130 | + this.id = id; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + public Map<String, Object> getItems() { |
| 135 | + return this.items; |
| 136 | + } |
| 137 | + |
| 138 | + public Object set(String key, Object item) { |
| 139 | + if (item == null) { |
| 140 | + throw new IllegalArgumentException("item may not be null."); |
| 141 | + } |
| 142 | + if (key == null) { |
| 143 | + throw new IllegalArgumentException("key may not be null."); |
| 144 | + } |
| 145 | + return this.items.put(key, item); |
| 146 | + } |
| 147 | + |
| 148 | + public void set(Map<String, Object> items) { |
| 149 | + if (items == null) { |
| 150 | + throw new IllegalArgumentException("items may not be null."); |
| 151 | + } |
| 152 | + this.items.putAll(items); |
| 153 | + } |
| 154 | + |
| 155 | + public Object remove(Object key) { |
| 156 | + return this.items.remove(key); |
| 157 | + } |
| 158 | + |
| 159 | + public void removeAll() { |
| 160 | + this.items.clear(); |
| 161 | + } |
| 162 | + |
| 163 | + public CompoundValue build() { |
| 164 | + return new MultiValue(this.id, this.type, this.items); |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments