Coverage Report - org.kuali.rice.core.api.mo.common.ImmutableKeyValue
 
Classes in this File Line Coverage Branch Coverage Complexity
ImmutableKeyValue
0%
0/31
0%
0/8
1.615
ImmutableKeyValue$Constants
0%
0/2
N/A
1.615
ImmutableKeyValue$Elements
0%
0/1
N/A
1.615
 
 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  
  * An immutable key value class. This class is has a comparison order
 24  
  * that string case-insensitive compares value then key.
 25  
  *
 26  
  * <p>
 27  
  * This class cannot be constructed with a null key but is allowed to have null values.
 28  
  * </p>
 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  
      * This constructor should never be called except during JAXB unmarshalling.
 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  
      * Creates a ImmutableKeyValue from a {@link Map.Entry}.
 70  
      * @param entry cannot be null
 71  
      * @return key value
 72  
      * @throws IllegalArgumentException if the entry or the entry's key is null
 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  
      * Creates a ImmutableKeyValue from strings.
 83  
      * @param key cannot be null
 84  
      * @param value can be null
 85  
      * @return key value
 86  
      * @throws IllegalArgumentException if the key is null
 87  
      */
 88  
     public static ImmutableKeyValue fromStrings(String key, String value) {
 89  
         //key validation done in private ctor
 90  0
         return new ImmutableKeyValue(key, value);
 91  
     }
 92  
 
 93  
     /**
 94  
      * Creates a ImmutableKeyValue from a {@link KeyValue}.
 95  
      * @param keyValue cannot be null
 96  
      * @return key value
 97  
      * @throws IllegalArgumentException if the keyValue or the keyValue's key is null
 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  
      * Converts key value to a mutable {@link Map} containing a single item.
 108  
      * The map returned is disconnected from this KeyValue class.
 109  
      *
 110  
      * @return a Map
 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  
      * Converts key value to a mutable {@link Map.Entry}.
 120  
      * The entry returned is disconnected from this KeyValue class.
 121  
      *
 122  
      * @return an entry
 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  
      * Defines some internal constants used on this class.
 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  
      * A private class which exposes constants which define the XML element names to use
 176  
      * when this object is marshalled to XML.
 177  
      */
 178  0
     static class Elements {
 179  
         final static String KEY = "key";
 180  
         final static String VALUE = "value";
 181  
     }
 182  
 }