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

changeset 0
fb9019fb1bf7
child 3
73bdfa70b04e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/net/fortuna/ical4j/model/NumberList.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,154 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012, Ben Fortuna
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + *
    1.12 + *  o Redistributions of source code must retain the above copyright
    1.13 + * notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *  o Redistributions in binary form must reproduce the above copyright
    1.16 + * notice, this list of conditions and the following disclaimer in the
    1.17 + * documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + *  o Neither the name of Ben Fortuna nor the names of any other contributors
    1.20 + * may be used to endorse or promote products derived from this software
    1.21 + * without specific prior written permission.
    1.22 + *
    1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.34 + */
    1.35 +package net.fortuna.ical4j.model;
    1.36 +
    1.37 +import java.io.Serializable;
    1.38 +import java.util.ArrayList;
    1.39 +import java.util.Iterator;
    1.40 +import java.util.StringTokenizer;
    1.41 +
    1.42 +import net.fortuna.ical4j.util.Numbers;
    1.43 +
    1.44 +/**
    1.45 + * $Id$ [29-May-2004]
    1.46 + *
    1.47 + * Defines a list of numbers.
    1.48 + * 
    1.49 + * @author Ben Fortuna
    1.50 + */
    1.51 +public class NumberList extends ArrayList implements Serializable {
    1.52 +    
    1.53 +    private static final long serialVersionUID = -1667481795613729889L;
    1.54 +
    1.55 +    private final int minValue;
    1.56 +    
    1.57 +    private final int maxValue;
    1.58 +
    1.59 +    private final boolean allowsNegativeValues;
    1.60 +    
    1.61 +    /**
    1.62 +     * Default constructor.
    1.63 +     */
    1.64 +    public NumberList() {
    1.65 +    	this(Integer.MIN_VALUE, Integer.MAX_VALUE, true);
    1.66 +    }
    1.67 +
    1.68 +    /**
    1.69 +     * Constructor with limits.
    1.70 +     * @param minValue the minimum allowable value
    1.71 +     * @param maxValue the maximum allowable value
    1.72 +     * @param allowsNegativeValues indicates whether negative values are allowed
    1.73 +     */
    1.74 +    public NumberList(int minValue, int maxValue, boolean allowsNegativeValues) {
    1.75 +    	this.minValue = minValue;
    1.76 +    	this.maxValue = maxValue;
    1.77 +        this.allowsNegativeValues = allowsNegativeValues;
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Constructor.
    1.82 +     * @param aString a string representation of a number list
    1.83 +     */
    1.84 +    public NumberList(final String aString) {
    1.85 +    	this(aString, Integer.MIN_VALUE, Integer.MAX_VALUE, true);
    1.86 +    }
    1.87 +    
    1.88 +    /**
    1.89 +     * @param aString a string representation of a number list
    1.90 +     * @param minValue the minimum allowable value
    1.91 +     * @param maxValue the maximum allowable value
    1.92 +     * @param allowsNegativeValues indicates whether negative values are allowed
    1.93 +     */
    1.94 +    public NumberList(final String aString, int minValue, int maxValue, boolean allowsNegativeValues) {
    1.95 +    	this(minValue, maxValue, allowsNegativeValues);
    1.96 +        final StringTokenizer t = new StringTokenizer(aString, ",");
    1.97 +        while (t.hasMoreTokens()) {
    1.98 +        	final int value = Numbers.parseInt(t.nextToken());
    1.99 +            add(new Integer(value));
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    /**
   1.104 +     * @param aNumber a number to add to the list
   1.105 +     * @return true if the number was added, otherwise false
   1.106 +     */
   1.107 +    public final boolean add(final Integer aNumber) {
   1.108 +        int abs = aNumber.intValue();
   1.109 +        if ((abs >> 31 | -abs >>> 31) < 0) {
   1.110 +            if (!allowsNegativeValues) {
   1.111 +                throw new IllegalArgumentException("Negative value not allowed: " + aNumber);
   1.112 +            }
   1.113 +            abs = Math.abs(abs);
   1.114 +        }
   1.115 +    	if (abs < minValue || abs > maxValue) {
   1.116 +    		throw new IllegalArgumentException(
   1.117 +    		        "Value not in range [" + minValue + ".." + maxValue + "]: " + aNumber);
   1.118 +    	}
   1.119 +        return add((Object) aNumber);
   1.120 +    }
   1.121 +    
   1.122 +    /**
   1.123 +     * Overrides superclass to throw an <code>IllegalArgumentException</code>
   1.124 +     * where argument is not a <code>java.lang.Integer</code>.
   1.125 +     * @param arg0 an object to add
   1.126 +     * @return true if the object was added, otherwise false
   1.127 +     * @see List#add(E)
   1.128 +     */
   1.129 +    public final boolean add(final Object arg0) {
   1.130 +        if (!(arg0 instanceof Integer)) {
   1.131 +            throw new IllegalArgumentException("Argument not a " + Integer.class.getName());
   1.132 +        }
   1.133 +        return super.add(arg0);
   1.134 +    }
   1.135 +
   1.136 +    /**
   1.137 +     * @param aNumber a number to remove from the list
   1.138 +     * @return true if the number was removed, otherwise false
   1.139 +     */
   1.140 +    public final boolean remove(final Integer aNumber) {
   1.141 +        return remove((Object) aNumber);
   1.142 +    }
   1.143 +
   1.144 +    /**
   1.145 +     * {@inheritDoc}
   1.146 +     */
   1.147 +    public final String toString() {
   1.148 +        final StringBuffer b = new StringBuffer();
   1.149 +        for (final Iterator i = iterator(); i.hasNext();) {
   1.150 +            b.append(i.next());
   1.151 +            if (i.hasNext()) {
   1.152 +                b.append(',');
   1.153 +            }
   1.154 +        }
   1.155 +        return b.toString();
   1.156 +    }
   1.157 +}

mercurial