0ea535129b68a9aff0ad78029ddca8fd59d7fe06
[public/netxms.git] / src / java / netxms-client / src / main / java / org / netxms / client / maps / elements / NetworkMapElement.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2010 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.client.maps.elements;
20
21 import org.netxms.base.NXCPMessage;
22
23 /**
24 * Network map element.
25 *
26 */
27 public class NetworkMapElement
28 {
29 public static final int MAP_ELEMENT_GENERIC = 0;
30 public static final int MAP_ELEMENT_OBJECT = 1;
31 public static final int MAP_ELEMENT_DECORATION = 2;
32
33 protected long id;
34 protected int type;
35 protected int x;
36 protected int y;
37
38 /**
39 * Factory method for creating map element from NXCP message.
40 *
41 * @param msg NXCP message
42 * @param baseId base variable ID
43 * @return map element object
44 */
45 public static NetworkMapElement createMapElement(NXCPMessage msg, long baseId)
46 {
47 int type = msg.getVariableAsInteger(baseId + 1);
48 switch(type)
49 {
50 case MAP_ELEMENT_OBJECT:
51 return new NetworkMapObject(msg, baseId);
52 case MAP_ELEMENT_DECORATION:
53 return new NetworkMapDecoration(msg, baseId);
54 default:
55 return new NetworkMapElement(msg, baseId);
56 }
57 }
58
59 /**
60 * Create element from NXCP message.
61 *
62 * @param msg NXCP message
63 * @param baseId base variable ID
64 */
65 protected NetworkMapElement(NXCPMessage msg, long baseId)
66 {
67 id = msg.getVariableAsInt64(baseId);
68 type = msg.getVariableAsInteger(baseId + 1);
69 x = msg.getVariableAsInteger(baseId + 2);
70 y = msg.getVariableAsInteger(baseId + 3);
71 }
72
73 /**
74 * Create new generic element
75 */
76 public NetworkMapElement(long id)
77 {
78 this.id = id;
79 type = MAP_ELEMENT_GENERIC;
80 x = 0;
81 y = 0;
82 }
83
84 /**
85 * Fill NXCP message with element data
86 *
87 * @param msg NXCP message
88 * @param baseId base variable ID
89 */
90 public void fillMessage(NXCPMessage msg, long baseId)
91 {
92 msg.setVariableInt32(baseId, (int)id);
93 msg.setVariableInt16(baseId + 1, type);
94 msg.setVariableInt32(baseId + 2, x);
95 msg.setVariableInt32(baseId + 3, y);
96 }
97
98 /**
99 * @return the type
100 */
101 public int getType()
102 {
103 return type;
104 }
105
106 /**
107 * @return the x
108 */
109 public int getX()
110 {
111 return x;
112 }
113
114 /**
115 * @return the y
116 */
117 public int getY()
118 {
119 return y;
120 }
121
122 /**
123 * @return the id
124 */
125 public long getId()
126 {
127 return id;
128 }
129
130 /* (non-Javadoc)
131 * @see java.lang.Object#equals(java.lang.Object)
132 */
133 @Override
134 public boolean equals(Object obj)
135 {
136 if (obj instanceof NetworkMapElement)
137 return ((NetworkMapElement)obj).id == id;
138 return super.equals(obj);
139 }
140
141 /* (non-Javadoc)
142 * @see java.lang.Object#hashCode()
143 */
144 @Override
145 public int hashCode()
146 {
147 return (int)id;
148 }
149 }