9ade348004c768abbd1340e46261eecb9fde8b2c
[jabaws.git] / datamodel / compbio / data / sequence / Range.java
1 /* Copyright (c) 2011 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.data.sequence;\r
19 \r
20 public class Range implements Comparable<Range> {\r
21 \r
22         public final int from;\r
23         public final int to;\r
24 \r
25         private Range() {\r
26                 // JAXB default constructor should not be used\r
27                 from = 0;\r
28                 to = from;\r
29         }\r
30 \r
31         public Range(int from, int to) {\r
32                 this.from = from;\r
33                 this.to = to;\r
34         }\r
35 \r
36         public Range(String[] twoElementAr) {\r
37                 if (twoElementAr == null || twoElementAr.length != 2) {\r
38                         throw new IllegalArgumentException();\r
39                 }\r
40                 this.from = Integer.parseInt(twoElementAr[0].trim());\r
41                 this.to = Integer.parseInt(twoElementAr[1].trim());\r
42         }\r
43         \r
44         // Daniel Check these get methods are ok with Jim\r
45         public int getFrom() { return from; }\r
46         public int getTo() { return to; }\r
47         \r
48         \r
49         @Override\r
50         public String toString() {\r
51                 return from + "-" + to;\r
52         }\r
53         \r
54 \r
55         @Override\r
56         public int hashCode() {\r
57                 final int prime = 31;\r
58                 int result = 1;\r
59                 result = prime * result + from;\r
60                 result = prime * result + to;\r
61                 return result;\r
62         }\r
63         @Override\r
64         public boolean equals(Object obj) {\r
65                 if (this == obj)\r
66                         return true;\r
67                 if (obj == null)\r
68                         return false;\r
69                 if (getClass() != obj.getClass())\r
70                         return false;\r
71                 Range other = (Range) obj;\r
72                 if (from != other.from)\r
73                         return false;\r
74                 if (to != other.to)\r
75                         return false;\r
76                 return true;\r
77         }\r
78 \r
79         // daniel wants to mess with method. this is how it was\r
80 //      @Override\r
81 //      public int compareTo(Range o) {\r
82 //              if (o == null)\r
83 //                      return 1;\r
84 //              return new Integer(this.from).compareTo(new Integer(o.from));\r
85 //      }\r
86         \r
87         @Override\r
88         public int compareTo(Range o) {\r
89                 if (o == null)\r
90                         return 1;\r
91                 if (new Integer(this.from).compareTo(new Integer(o.from)) != 0) {\r
92                         return new Integer(this.from).compareTo(new Integer(o.from));\r
93                 }\r
94                 else {\r
95                         return new Integer(this.to).compareTo(new Integer(o.to));\r
96                 }\r
97         }\r
98 \r
99 }\r