src/net/fortuna/ical4j/model/NumberList.java

Tue, 10 Feb 2015 19:58:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 19:58:00 +0100
changeset 4
45d57ecba757
parent 0
fb9019fb1bf7
permissions
-rw-r--r--

Upgrade the upgraded ical4j component to use org.apache.commons.lang3.

     1 /**
     2  * Copyright (c) 2012, Ben Fortuna
     3  * All rights reserved.
     4  *
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions
     7  * are met:
     8  *
     9  *  o Redistributions of source code must retain the above copyright
    10  * notice, this list of conditions and the following disclaimer.
    11  *
    12  *  o Redistributions in binary form must reproduce the above copyright
    13  * notice, this list of conditions and the following disclaimer in the
    14  * documentation and/or other materials provided with the distribution.
    15  *
    16  *  o Neither the name of Ben Fortuna nor the names of any other contributors
    17  * may be used to endorse or promote products derived from this software
    18  * without specific prior written permission.
    19  *
    20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31  */
    32 package net.fortuna.ical4j.model;
    34 import java.io.Serializable;
    35 import java.util.ArrayList;
    36 import java.util.Iterator;
    37 import java.util.StringTokenizer;
    39 import net.fortuna.ical4j.util.Numbers;
    41 /**
    42  * $Id$ [29-May-2004]
    43  *
    44  * Defines a list of numbers.
    45  * 
    46  * @author Ben Fortuna
    47  */
    48 public class NumberList extends ArrayList implements Serializable {
    50     private static final long serialVersionUID = -1667481795613729889L;
    52     private final int minValue;
    54     private final int maxValue;
    56     private final boolean allowsNegativeValues;
    58     /**
    59      * Default constructor.
    60      */
    61     public NumberList() {
    62     	this(Integer.MIN_VALUE, Integer.MAX_VALUE, true);
    63     }
    65     /**
    66      * Constructor with limits.
    67      * @param minValue the minimum allowable value
    68      * @param maxValue the maximum allowable value
    69      * @param allowsNegativeValues indicates whether negative values are allowed
    70      */
    71     public NumberList(int minValue, int maxValue, boolean allowsNegativeValues) {
    72     	this.minValue = minValue;
    73     	this.maxValue = maxValue;
    74         this.allowsNegativeValues = allowsNegativeValues;
    75     }
    77     /**
    78      * Constructor.
    79      * @param aString a string representation of a number list
    80      */
    81     public NumberList(final String aString) {
    82     	this(aString, Integer.MIN_VALUE, Integer.MAX_VALUE, true);
    83     }
    85     /**
    86      * @param aString a string representation of a number list
    87      * @param minValue the minimum allowable value
    88      * @param maxValue the maximum allowable value
    89      * @param allowsNegativeValues indicates whether negative values are allowed
    90      */
    91     public NumberList(final String aString, int minValue, int maxValue, boolean allowsNegativeValues) {
    92     	this(minValue, maxValue, allowsNegativeValues);
    93         final StringTokenizer t = new StringTokenizer(aString, ",");
    94         while (t.hasMoreTokens()) {
    95         	final int value = Numbers.parseInt(t.nextToken());
    96             add(new Integer(value));
    97         }
    98     }
   100     /**
   101      * @param aNumber a number to add to the list
   102      * @return true if the number was added, otherwise false
   103      */
   104     public final boolean add(final Integer aNumber) {
   105         int abs = aNumber.intValue();
   106         if ((abs >> 31 | -abs >>> 31) < 0) {
   107             if (!allowsNegativeValues) {
   108                 throw new IllegalArgumentException("Negative value not allowed: " + aNumber);
   109             }
   110             abs = Math.abs(abs);
   111         }
   112     	if (abs < minValue || abs > maxValue) {
   113     		throw new IllegalArgumentException(
   114     		        "Value not in range [" + minValue + ".." + maxValue + "]: " + aNumber);
   115     	}
   116         return add((Object) aNumber);
   117     }
   119     /**
   120      * Overrides superclass to throw an <code>IllegalArgumentException</code>
   121      * where argument is not a <code>java.lang.Integer</code>.
   122      * @param arg0 an object to add
   123      * @return true if the object was added, otherwise false
   124      * @see java.util.List#add(Object)
   125      */
   126     public final boolean add(final Object arg0) {
   127         if (!(arg0 instanceof Integer)) {
   128             throw new IllegalArgumentException("Argument not a " + Integer.class.getName());
   129         }
   130         return super.add(arg0);
   131     }
   133     /**
   134      * @param aNumber a number to remove from the list
   135      * @return true if the number was removed, otherwise false
   136      */
   137     public final boolean remove(final Integer aNumber) {
   138         return remove((Object) aNumber);
   139     }
   141     /**
   142      * {@inheritDoc}
   143      */
   144     public final String toString() {
   145         final StringBuffer b = new StringBuffer();
   146         for (final Iterator i = iterator(); i.hasNext();) {
   147             b.append(i.next());
   148             if (i.hasNext()) {
   149                 b.append(',');
   150             }
   151         }
   152         return b.toString();
   153     }
   154 }

mercurial