| 1 | |
package org.kuali.rice.core.api.mo.common; |
| 2 | |
|
| 3 | |
import java.util.Collection; |
| 4 | |
import java.util.HashMap; |
| 5 | |
import java.util.Map; |
| 6 | |
|
| 7 | |
import javax.xml.bind.annotation.XmlAccessType; |
| 8 | |
import javax.xml.bind.annotation.XmlAccessorType; |
| 9 | |
import javax.xml.bind.annotation.XmlAnyElement; |
| 10 | |
import javax.xml.bind.annotation.XmlElement; |
| 11 | |
import javax.xml.bind.annotation.XmlRootElement; |
| 12 | |
import javax.xml.bind.annotation.XmlType; |
| 13 | |
|
| 14 | |
import org.apache.commons.lang.builder.CompareToBuilder; |
| 15 | |
import org.apache.commons.lang.builder.EqualsBuilder; |
| 16 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
| 17 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 18 | |
import org.kuali.rice.core.api.CoreConstants; |
| 19 | |
import org.kuali.rice.core.util.KeyValue; |
| 20 | |
import org.w3c.dom.Element; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 0 | @XmlRootElement(name = ImmutableKeyValue.Constants.ROOT_ELEMENT_NAME) |
| 31 | |
@XmlAccessorType(XmlAccessType.NONE) |
| 32 | |
@XmlType(name = ImmutableKeyValue.Constants.TYPE_NAME, propOrder = { |
| 33 | |
ImmutableKeyValue.Elements.KEY, |
| 34 | |
ImmutableKeyValue.Elements.VALUE, |
| 35 | |
CoreConstants.CommonElements.FUTURE_ELEMENTS |
| 36 | |
}) |
| 37 | |
public class ImmutableKeyValue implements KeyValue, Comparable<KeyValue>{ |
| 38 | |
|
| 39 | |
private static final long serialVersionUID = -4004980108397166233L; |
| 40 | |
|
| 41 | |
@XmlElement(name = Elements.KEY, required = true) |
| 42 | |
private final String key; |
| 43 | |
|
| 44 | |
@XmlElement(name = Elements.VALUE, required = true) |
| 45 | |
private final String value; |
| 46 | |
|
| 47 | 0 | @SuppressWarnings("unused") |
| 48 | |
@XmlAnyElement |
| 49 | |
private final Collection<Element> _futureElements = null; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 0 | private ImmutableKeyValue() { |
| 55 | 0 | this.key = null; |
| 56 | 0 | this.value = null; |
| 57 | 0 | } |
| 58 | |
|
| 59 | 0 | private ImmutableKeyValue(String key, String value) { |
| 60 | 0 | if (key == null) { |
| 61 | 0 | throw new IllegalArgumentException("key is null"); |
| 62 | |
} |
| 63 | |
|
| 64 | 0 | this.key = key; |
| 65 | 0 | this.value = value; |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public static ImmutableKeyValue fromMapEntry(Map.Entry<String, String> entry) { |
| 75 | 0 | if (entry == null) { |
| 76 | 0 | throw new IllegalArgumentException("entry is null"); |
| 77 | |
} |
| 78 | 0 | return new ImmutableKeyValue(entry.getKey(), entry.getValue()); |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public static ImmutableKeyValue fromStrings(String key, String value) { |
| 89 | |
|
| 90 | 0 | return new ImmutableKeyValue(key, value); |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public static ImmutableKeyValue fromKeyValue(KeyValue keyValue) { |
| 100 | 0 | if (keyValue == null) { |
| 101 | 0 | throw new IllegalArgumentException("keyValue is null"); |
| 102 | |
} |
| 103 | 0 | return new ImmutableKeyValue(keyValue.getKey(), keyValue.getValue()); |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public Map<String, String> toMap() { |
| 113 | 0 | Map<String, String> m = new HashMap<String, String>(); |
| 114 | 0 | m.put(key, value); |
| 115 | 0 | return m; |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public Map.Entry<String, String> toMapEntry() { |
| 125 | 0 | return new HashMap.SimpleEntry<String, String>(key, value); |
| 126 | |
} |
| 127 | |
|
| 128 | |
@Override |
| 129 | |
public String getKey() { |
| 130 | 0 | return key; |
| 131 | |
} |
| 132 | |
|
| 133 | |
@Override |
| 134 | |
public String getValue() { |
| 135 | 0 | return value; |
| 136 | |
} |
| 137 | |
|
| 138 | |
@Override |
| 139 | |
public int compareTo(KeyValue o) { |
| 140 | 0 | if (o == null) { |
| 141 | 0 | throw new NullPointerException("o is null"); |
| 142 | |
} |
| 143 | |
|
| 144 | 0 | return new CompareToBuilder() |
| 145 | |
.append(this.getValue(), o.getValue(), String.CASE_INSENSITIVE_ORDER) |
| 146 | |
.append(this.getKey(), o.getKey(), String.CASE_INSENSITIVE_ORDER) |
| 147 | |
.toComparison(); |
| 148 | |
} |
| 149 | |
|
| 150 | |
@Override |
| 151 | |
public int hashCode() { |
| 152 | 0 | return HashCodeBuilder.reflectionHashCode(this, Constants.HASH_CODE_EQUALS_EXCLUDE); |
| 153 | |
} |
| 154 | |
|
| 155 | |
@Override |
| 156 | |
public boolean equals(Object obj) { |
| 157 | 0 | return EqualsBuilder.reflectionEquals(obj, this, Constants.HASH_CODE_EQUALS_EXCLUDE); |
| 158 | |
} |
| 159 | |
|
| 160 | |
@Override |
| 161 | |
public String toString() { |
| 162 | 0 | return ToStringBuilder.reflectionToString(this); |
| 163 | |
} |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | 0 | static class Constants { |
| 169 | |
final static String ROOT_ELEMENT_NAME = "keyValue"; |
| 170 | |
final static String TYPE_NAME = "KeyValueType"; |
| 171 | 0 | final static String[] HASH_CODE_EQUALS_EXCLUDE = {CoreConstants.CommonElements.FUTURE_ELEMENTS}; |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | 0 | static class Elements { |
| 179 | |
final static String KEY = "key"; |
| 180 | |
final static String VALUE = "value"; |
| 181 | |
} |
| 182 | |
} |