Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6943ece4bb4d7daadf21bc4829db146bd84a7743 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.importing;

/**
 * @author Robert A. Fisher
 */
public class ReqNumbering implements Comparable<ReqNumbering> {
   private static final int ZERO_BASED_NUMBERING = 2;
   private static final int ONE_BASED_NUMBERING = 1;

   private final String number;
   private final String[] values;

   /**
    * Requirement Numbering
    *
    * @note When a number with a separator - is used, i.e. 1.2-1. All - are replaced with . at construction.
    * @param number
    */
   public ReqNumbering(String number) {
      //When additional separators are used (- instead of .)
      this.number = number.replace("-", ".");
      values = tokenize();
   }

   public String getNumberString() {
      return number;
   }

   /**
    * @return returns whether the numbering argument is a child of this number
    */
   public boolean isChild(ReqNumbering numbering) {
      String[] numberVals = numbering.values;

      switch (numberVals.length - values.length) {
         case ZERO_BASED_NUMBERING:
            if (!numberVals[numberVals.length - 2].equals("0")) {
               return false;
            }
            break;
         case ONE_BASED_NUMBERING:
            break;
         default:
            return false;
      }

      for (int i = 0; i < values.length; i++) {
         if (!values[i].equals(numberVals[i])) {
            return false;
         }
      }
      return true;
   }

   public String[] tokenize() {
      String[] returnVal = number.split("\\.");

      // If the very last token is a 0, then chop it off
      if (returnVal[returnVal.length - 1].equals("0")) {
         String[] temp = new String[returnVal.length - 1];
         System.arraycopy(returnVal, 0, temp, 0, temp.length);
         returnVal = temp;
      }
      return returnVal;
   }

   @Override
   public int compareTo(ReqNumbering o) {
      for (int i = 0; i < Math.max(values.length, o.values.length); i++) {
         int thisValue = getValue(i);
         int oValue = o.getValue(i);
         if (thisValue > oValue) {
            return 1;
         } else if (thisValue < oValue) {
            return -1;
         }
      }

      return 0;
   }

   @Override
   public boolean equals(Object o) {
      if (!(o instanceof ReqNumbering)) {
         return false;
      }
      return this.compareTo((ReqNumbering) o) == 0;
   }

   @Override
   public int hashCode() {
      return number.hashCode();
   }

   private int getValue(int index) {
      if (index <= values.length - 1) {
         return Integer.parseInt(values[index]);
      } else {
         return 0;
      }
   }

   @Override
   public String toString() {
      return number;
   }
}

Back to the top