Debian Codegraph @

src/com/jidesoft/range/Category.java

raw
1 /*
2 * @(#)Category.java
3 *
4 * 2002 - 2012 JIDE Software Incorporated. All rights reserved.
5 * Copyright (c) 2005 - 2012 Catalysoft Limited. All rights reserved.
6 */
7
8 package com.jidesoft.range;
9
10 import java.io.Serializable;
11
12 /**
13 * This class is really an adapter because it takes any object and allows it to be used as a Category.
14 *
15 * @author Simon White ([email protected])
16 */
17 public class Category<T> implements Positionable, Serializable {
18 private static final long serialVersionUID = -273502456506105902L;
19 private String _name;
20 private T _value;
21 private CategoryRange<T> _range;
22
23 public Category(String name, T value) {
24 setName(name);
25 _value = value;
26 }
27
28 public Category(String name, T value, CategoryRange<T> range) {
29 setName(name);
30 _value = value;
31 _range = range;
32 }
33
34 public Category(T value) {
35 _value = value;
36 }
37
38 public Category(T value, CategoryRange<T> range) {
39 _value = value;
40 _range = range;
41 }
42
43 public CategoryRange<T> getRange() {
44 return _range;
45 }
46
47 public void setRange(CategoryRange<T> range) {
48 _range = range;
49 }
50
51 /**
52 * Returns the position of the category, which will be used to decide where to place a category along an axis.
53 * @return the position of the category
54 */
55 public double position() {
56 if (_range == null) {
57 throw new IllegalStateException("Cannot compute position for a category that does not belong to a range");
58 }
59 return _range.position(_value);
60 }
61
62 /**
63 * Returns the original value of the category
64 * @return the value of the category
65 */
66 public T getValue() {
67 return _value;
68 }
69
70 /**
71 * Returns the name of the category which, when set, will be used as a label for the category.
72 * If it is not set explicitly, then the toString() of the category value will be used instead.
73 * @return the name of the category.
74 */
75 public String getName() {
76 if (_name == null) {
77 return _value.toString();
78 }
79 return _name;
80 }
81
82 /**
83 * Specify the name of the category
84 * @param name the name of the category
85 * @see #getName()
86 */
87 private void setName(String name) {
88 _name = name;
89 }
90
91 public int compareTo(Positionable o) {
92 double otherPosition = o.position();
93 double position = position();
94 if (position < otherPosition) {
95 return -1;
96 }
97 else if (position > otherPosition) {
98 return 1;
99 }
100 else {
101 return 0;
102 }
103 }
104
105
106
107 @Override
108 public int hashCode() {
109 final int prime = 31;
110 int result = 1;
111 result = prime * result + ((_name == null) ? 0 : _name.hashCode());
112 result = prime * result + ((_value == null) ? 0 : _value.hashCode());
113 return result;
114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (this == obj)
119 return true;
120 if (obj == null)
121 return false;
122 if (getClass() != obj.getClass())
123 return false;
124 Category other = (Category) obj;
125 if (_name == null) {
126 if (other._name != null)
127 return false;
128 } else if (!_name.equals(other._name))
129 return false;
130 if (_value == null) {
131 if (other._value != null)
132 return false;
133 } else if (!_value.equals(other._value))
134 return false;
135 return true;
136 }
137
138 @Override
139 public String toString() {
140 return String.format("#<Category name='%s' value='%s'>", _name, _value.toString());
141 }
142
143 }