001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.hivemind;
016    
017    /**
018     * Identifies the number of contributions allowed to a configuration extension point.
019     * 
020     * @author Howard Lewis Ship
021     */
022    public abstract class Occurances
023    {
024        /**
025         * An unbounded number, zero or more.
026         */
027        public static final Occurances UNBOUNDED = new Occurances("UNBOUNDED")
028        {
029            public boolean inRange(int count)
030            {
031                return true;
032            }
033        };
034    
035        /**
036         * Optional, may be zero or one, but not more.
037         */
038    
039        public static final Occurances OPTIONAL = new Occurances("OPTIONAL")
040        {
041            public boolean inRange(int count)
042            {
043                return count < 2;
044            }
045        };
046    
047        /**
048         * Exactly one is required.
049         */
050    
051        public static final Occurances REQUIRED = new Occurances("REQUIRED")
052        {
053            public boolean inRange(int count)
054            {
055                return count == 1;
056            }
057        };
058    
059        /**
060         * At least one is required.
061         */
062    
063        public static final Occurances ONE_PLUS = new Occurances("ONE_PLUS")
064        {
065            public boolean inRange(int count)
066            {
067                return count > 0;
068            }
069        };
070    
071        public static final Occurances NONE = new Occurances("NONE")
072        {
073            public boolean inRange(int count)
074            {
075                return count == 0;
076            }
077        };
078    
079        private String _name;
080    
081        private Occurances(String name)
082        {
083            _name = name;
084        }
085    
086        public String getName()
087        {
088            return _name;
089        }
090    
091        public String toString()
092        {
093            return "Occurances[" + _name + "]";
094        }
095    
096        /**
097         * Validates that an actual count is in range for the particular Occurances count.
098         * 
099         * @param count
100         *            the number of items to check. Should be zero or greater.
101         * @return true if count is a valid number in accordance to the range, false otherwise
102         */
103        public abstract boolean inRange(int count);
104    
105    }