Coverage Report - de.smartics.properties.spi.core.value.CollectionPropertyValueRange
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionPropertyValueRange
0%
0/38
0%
0/16
2.091
 
 1  
 /*
 2  
  * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
 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 de.smartics.properties.spi.core.value;
 17  
 
 18  
 import java.io.Serializable;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.LinkedHashMap;
 22  
 import java.util.List;
 23  
 import java.util.Locale;
 24  
 import java.util.Map;
 25  
 
 26  
 import de.smartics.properties.api.core.domain.PropertyValueRange;
 27  
 import de.smartics.util.lang.Arg;
 28  
 import de.smartics.util.lang.NullArgumentException;
 29  
 
 30  
 /**
 31  
  * Defines a set of valid enumerable elements for values of a property.
 32  
  *
 33  
  * @param <T> the type of the property values within the given range. Please
 34  
  *          note that the type is required to be serializable. Otherwise
 35  
  *          instances of this class are also not serializable.
 36  
  */
 37  
 public final class CollectionPropertyValueRange<T> implements
 38  
     PropertyValueRange<T>, Serializable
 39  
 {
 40  
   // ********************************* Fields *********************************
 41  
 
 42  
   // --- constants ------------------------------------------------------------
 43  
 
 44  
   /**
 45  
    * The class version identifier.
 46  
    */
 47  
   private static final long serialVersionUID = 1L;
 48  
 
 49  
   // --- members --------------------------------------------------------------
 50  
 
 51  
   /**
 52  
    * The values mapped by their string representation to their value instance.
 53  
    *
 54  
    * @serial
 55  
    */
 56  
   private final Map<String, T> valueRange;
 57  
 
 58  
   // ****************************** Initializer *******************************
 59  
 
 60  
   // ****************************** Constructors ******************************
 61  
 
 62  
   /**
 63  
    * Constructor accepting an array of values.
 64  
    *
 65  
    * @param values the allowed values for this value range.
 66  
    * @throws NullArgumentException if {@code values} is <code>null</code>.
 67  
    */
 68  
   public CollectionPropertyValueRange(final T[] values)
 69  
     throws NullArgumentException
 70  0
   {
 71  0
     Arg.checkNotNull("values", values);
 72  0
     this.valueRange = createMap(values);
 73  0
   }
 74  
 
 75  
   /**
 76  
    * Constructor accepting a collection of values.
 77  
    *
 78  
    * @param values the allowed values for this value range.
 79  
    * @throws NullArgumentException if {@code values} is <code>null</code>.
 80  
    */
 81  
   public CollectionPropertyValueRange(final Collection<T> values)
 82  
     throws NullArgumentException
 83  0
   {
 84  0
     Arg.checkNotNull("values", values);
 85  0
     this.valueRange = createMap(values);
 86  0
   }
 87  
 
 88  
   // ****************************** Inner Classes *****************************
 89  
 
 90  
   // ********************************* Methods ********************************
 91  
 
 92  
   // --- init -----------------------------------------------------------------
 93  
 
 94  
   private static <T> Map<String, T> createMap(final T[] values)
 95  
   {
 96  0
     final Map<String, T> map = new LinkedHashMap<String, T>(values.length);
 97  0
     for (T value : values)
 98  
     {
 99  0
       map.put(String.valueOf(value), value);
 100  
     }
 101  0
     return map;
 102  
   }
 103  
 
 104  
   private static <T> Map<String, T> createMap(final Collection<T> values)
 105  
   {
 106  0
     final Map<String, T> map = new LinkedHashMap<String, T>(values.size());
 107  0
     for (T value : values)
 108  
     {
 109  0
       map.put(String.valueOf(value), value);
 110  
     }
 111  0
     return map;
 112  
   }
 113  
 
 114  
   // --- get&set --------------------------------------------------------------
 115  
 
 116  
   // --- business -------------------------------------------------------------
 117  
 
 118  
   @Override
 119  
   public List<T> getValues()
 120  
   {
 121  0
     final List<T> list = new ArrayList<T>(valueRange.values());
 122  0
     return list;
 123  
   }
 124  
 
 125  
   @Override
 126  
   public T fromString(final String valueAsString)
 127  
     throws IllegalArgumentException
 128  
   {
 129  0
     final T value = valueRange.get(valueAsString);
 130  0
     if (value != null)
 131  
     {
 132  0
       return value;
 133  
     }
 134  
     else
 135  
     {
 136  0
       throw new IllegalArgumentException(
 137  
           "The value '" + valueAsString
 138  
               + "' is not a valid value. Valid values are "
 139  
               + valueRange.values());
 140  
     }
 141  
   }
 142  
 
 143  
   @Override
 144  
   public boolean isCommented()
 145  
   {
 146  0
     return false;
 147  
   }
 148  
 
 149  
   // --- object basics --------------------------------------------------------
 150  
 
 151  
   /**
 152  
    * Returns the hash code of the object.
 153  
    *
 154  
    * @return the hash code.
 155  
    */
 156  
   @Override
 157  
   public int hashCode()
 158  
   {
 159  0
     return valueRange.hashCode();
 160  
   }
 161  
 
 162  
   /**
 163  
    * Returns <code>true</code> if the given object is semantically equal to the
 164  
    * given object, <code>false</code> otherwise.
 165  
    *
 166  
    * @param object the instance to compare to.
 167  
    * @return <code>true</code> if the given object is semantically equal to the
 168  
    *         given object, <code>false</code> otherwise.
 169  
    */
 170  
   @Override
 171  
   public boolean equals(final Object object)
 172  
   {
 173  0
     if (this == object)
 174  
     {
 175  0
       return true;
 176  
     }
 177  0
     else if (object == null || getClass() != object.getClass())
 178  
     {
 179  0
       return false;
 180  
     }
 181  
 
 182  0
     final CollectionPropertyValueRange<?> other =
 183  
         (CollectionPropertyValueRange<?>) object;
 184  
 
 185  0
     return (valueRange.equals(other.valueRange));
 186  
   }
 187  
 
 188  
   @Override
 189  
   public String toString(final Locale locale)
 190  
   {
 191  0
     final StringBuilder buffer = new StringBuilder(64);
 192  0
     for (String value : valueRange.keySet())
 193  
     {
 194  0
       buffer.append(value).append(", ");
 195  
     }
 196  
 
 197  0
     final int length = buffer.length();
 198  0
     if (length > 0)
 199  
     {
 200  0
       buffer.setLength(length - 2);
 201  
     }
 202  
 
 203  0
     return buffer.toString();
 204  
   }
 205  
 
 206  
   @Override
 207  
   public String toString()
 208  
   {
 209  0
     return toString(Locale.getDefault());
 210  
   }
 211  
 }