View Javadoc

1   /*$Id: Cardinality.java,v 1.1 2004/08/16 11:03:07 nw Exp $
2    * Created on 16-Aug-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.description;
12  
13  /*** models the cardinality - i.e. number of times it may occur - of a parameter.
14   * @author Noel Winstanley nw@jb.man.ac.uk 16-Aug-2004
15   *
16   */
17  public class Cardinality {
18  
19   /***
20    *  Construct a new Cardinality
21    * @param min minimum times the parameter must occur
22    * @param max maximum tmes the parameter must occur.
23    * @see #UNBOUNDED
24    */
25      public Cardinality(int min,int max) {
26          this.min = min;
27          this.max =max;
28      }
29      
30      private final int min;
31      private final int max;
32      /*** constant value to indicate unbounded max occurs */
33      public final static int UNBOUNDED = 0;
34      
35      /*** inspect the minimum number of occurences required of a parameter */
36      public int getMinOccurs() {
37          return min;
38      }
39      
40      /*** inspect the maximum number of occurrences required of a parameter */
41      public int getMaxOccurs() {
42          return max;
43      }
44      // constants for some common varieties
45      
46      /*** cardinality of a required parameter */
47      public final static Cardinality MANDATORY = new Cardinality(1,1);
48      /*** cardinality of an optional paramter */
49      public final static Cardinality OPTIONAL = new Cardinality(0,1);
50      /*** cardinality of a repeated parameter that must occur at least once */
51      public final static Cardinality MANDATORY_REPEATED = new Cardinality(1,UNBOUNDED);
52      /*** cardinality of a repeated parameter thay may occur 0, 1 or many times */
53      public final static Cardinality OPTIONAL_REPEATED = new Cardinality(0,UNBOUNDED);
54      
55  
56      /***
57       * Returns <code>true</code> if this <code>Cardinality</code> is the same as the o argument.
58       *
59       * @return <code>true</code> if this <code>Cardinality</code> is the same as the o argument.
60       */
61      public boolean equals(Object o) {
62          if (this == o) {
63              return true;
64          }
65          if (o == null) {
66              return false;
67          }
68          if (o.getClass() != getClass()) {
69              return false;
70          }
71          Cardinality castedObj = (Cardinality) o;
72          return ((this.min == castedObj.min) && (this.max == castedObj.max));
73      }
74      /***
75       * Override hashCode.
76       *
77       * @return the Objects hashcode.
78       */
79      public int hashCode() {
80          int hashCode = 1;
81          hashCode = 31 * hashCode + min;
82          hashCode = 31 * hashCode + max;
83          return hashCode;
84      }
85      public String toString() {
86          StringBuffer buffer = new StringBuffer();
87          buffer.append("[Cardinality:");
88          buffer.append(" min: ");
89          buffer.append(min);
90          buffer.append(" max: ");
91          buffer.append(max);
92          buffer.append("]");
93          return buffer.toString();
94      }
95  }
96  
97  
98  /* 
99  $Log: Cardinality.java,v $
100 Revision 1.1  2004/08/16 11:03:07  nw
101 added classes to model cardinality of prefs.
102  
103 */