1 /*
2 Copyright (C) 2007 Dirk Huenniger
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 package org.indi.objects;
20
21 import java.io.Serializable;
22 import java.util.LinkedList;
23
24 /**
25 * A class representing a single indielement to be accomodated by an indivector
26 *
27 * @author Dirk Hünniger
28 *
29 */
30 public abstract class Element extends Object<Object> implements Serializable {
31 /**
32 *
33 */
34 private static final long serialVersionUID = 1L;
35
36 /**
37 *
38 * @return the type of the indi element
39 */
40 protected abstract String getType();
41
42 /**
43 * the value of the indielement
44 */
45 protected String value;
46 /**
47 * the label of the indielement to be uses in a GUI
48 */
49 protected String label;
50 /**
51 * the name of the indielement to identify it uniquely
52 */
53 protected String name;
54
55 /**
56 * class constructor
57 *
58 * @param name
59 * the name of the indielement (unique identiyer)
60 * @param label
61 * the label of the indielement (for use in GUI)
62 * @param value
63 * the indielement (the data it carries)
64 */
65 public Element(String name, String label, String value) {
66 super(new LinkedList<Object>());
67 this.label = label;
68 this.value = value;
69 this.name = name;
70 }
71
72 /**
73 * Get the opening XML tag for the element
74 *
75 * @param type
76 * the transfertype to be used
77 * @return the generated opening tag
78 */
79 public String getStartTag(TransferType type) {
80 switch (type) {
81 case Def:
82 return "<def" + getType() + " name=\"" + this.name + "\" label=\""
83 + this.label + "\"> ";
84 default:
85 return "<one" + getType() + " name=\"" + this.name + "\"> ";
86 }
87 }
88
89 /**
90 * Get the closing XML tag for the element
91 *
92 * @param type
93 * the transfertype to be used
94 * @return the generated closing tag
95 */
96 public String getEndTag(TransferType type) {
97 switch (type) {
98 case Def:
99 return "</def" + getType() + "> ";
100 default:
101 return "</one" + getType() + "> ";
102 }
103 }
104
105 /**
106 * get the value of the indielement
107 *
108 * @param type
109 * the treansfertype to be used
110 * @return the value of the indiobject
111 */
112 public String getValue(TransferType type) {
113 return this.value;
114 }
115
116 /**
117 * set the value of the indielement
118 *
119 * @param value
120 * the new value to be set
121 */
122 public void setValue(String value) {
123 this.value = value;
124 }
125
126 /**
127 *
128 * @param type
129 * the transfertype to be used
130 * @return an xml representation of the indielement
131 */
132 public String getXML(TransferType type) {
133 return getStartTag(type) + getValue(type) + getEndTag(type);
134 }
135
136 /**
137 * @return the name of the indielement
138 */
139 public String getName() {
140 return this.name;
141 }
142
143 @Override
144 public boolean oEquals(Object o) {
145 if (o instanceof Element) {
146 Element e = (Element) o;
147 return this.name.equals(e.name) && getType().equals(e.getType());
148 } else {
149 return false;
150 }
151 }
152
153 @Override
154 public int hashCode() {
155 return this.name.hashCode();
156 };
157 }