Skip to content

Commit cc12943

Browse files
Use hashmap for storing GXProperties (#840)
* Use hashmap for storing GXProperties Issue:107267 * Add no argument constructor
1 parent 1eb9660 commit cc12943

File tree

2 files changed

+111
-163
lines changed

2 files changed

+111
-163
lines changed
Lines changed: 101 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.genexus.util;
22

3-
import java.util.Vector;
3+
import java.util.LinkedHashMap;
44

55
import com.genexus.internet.IGxJSONSerializable;
66

@@ -10,188 +10,131 @@
1010
import com.genexus.SdtMessages_Message;
1111
import com.genexus.GXBaseCollection;
1212
import java.util.Iterator;
13+
import java.util.Map;
1314

14-
public class GXProperties implements IGxJSONSerializable{
15-
private Vector<GXProperty> vector = new Vector<GXProperty>();
16-
private boolean eof;
17-
private int lastElement;
18-
19-
public GXProperties() {
20-
}
21-
22-
public void set(String name, String value)
23-
{
24-
put(name, value);
25-
}
26-
27-
public void add(String name, String value)
28-
{
29-
addToTheEnd(name, value);
30-
}
31-
32-
public void put(String name, String value)
33-
{
34-
int index = findElement(name);
35-
if ( index >= 0)
36-
{
37-
vector.elementAt(index).setValue(value);
38-
}
39-
else
40-
{
41-
addToTheEnd(name, value);
42-
}
43-
}
44-
public String toString() {
45-
StringBuilder builder = new StringBuilder();
46-
for (GXProperty property : vector) {
47-
builder.append(property.getValue());
48-
}
49-
return builder.toString();
50-
}
51-
private void addToTheEnd(String name, String value){
52-
53-
GXProperty prop = new GXProperty();
54-
prop.setKey(name);
55-
prop.setValue(value);
56-
vector.addElement(prop);
57-
}
58-
public String get(String name)
59-
{
60-
int index = findElement(name);
61-
if (index >= 0)
62-
return vector.elementAt(index).getValue();
63-
else
64-
return "";
65-
}
66-
67-
public void remove(String name)
68-
{
69-
int index = findElement(name);
70-
if (index >= 0)
71-
vector.removeElementAt(index);
72-
}
73-
74-
public boolean containsKey(String name)
75-
{
76-
if (findElement(name) == -1)
77-
return false;
78-
return true;
79-
}
80-
81-
private int findElement(String name)
82-
{
83-
int i = 0;
84-
while (count() > i)
85-
{
86-
if (item(i).getKey().equalsIgnoreCase(name))
87-
return i;
88-
i++;
89-
}
90-
return -1;
91-
}
92-
93-
public GXProperty item(int i)
94-
{
95-
return vector.elementAt(i);
96-
}
97-
98-
public int getCount()
99-
{
15+
public class GXProperties implements IGxJSONSerializable {
16+
private LinkedHashMap < String, GXProperty > properties = new LinkedHashMap < > ();
17+
private boolean eof;
18+
private int lastElement;
19+
20+
public GXProperties() {}
21+
22+
public void set(String name, String value) {
23+
this.put(name, value);
24+
}
25+
26+
public void add(String name, String value) {
27+
properties.put(name, new GXProperty(name, value));
28+
}
29+
30+
public void put(String name, String value) {
31+
properties.put(name, new GXProperty(name, value));
32+
}
33+
public String toString() {
34+
StringBuilder builder = new StringBuilder();
35+
for (GXProperty property: properties.values()) {
36+
builder.append(property.getValue());
37+
}
38+
return builder.toString();
39+
}
40+
41+
public String get(String name) {
42+
return containsKey(name) ? properties.get(name).getValue() : "";
43+
}
44+
45+
public void remove(String name) {
46+
properties.remove(name);
47+
}
48+
49+
public boolean containsKey(String name) {
50+
return properties.containsKey(name);
51+
}
52+
53+
public GXProperty item(int i) {
54+
int counter = 0;
55+
for (Map.Entry < String, GXProperty > entry: properties.entrySet()) {
56+
if (counter++ == i) {
57+
return entry.getValue();
58+
}
59+
}
60+
throw new IndexOutOfBoundsException("The provided index is larger than the amount of items stored");
61+
}
62+
63+
public int getCount() {
10064
return count();
10165
}
10266

103-
public int count()
104-
{
105-
return vector.size();
106-
}
107-
108-
public void clear()
109-
{
110-
vector.removeAllElements();
111-
}
112-
113-
public GXProperty first()
114-
{
115-
eof = false;
116-
if (count() > 0)
117-
{
118-
lastElement = 0;
119-
return vector.elementAt(0);
120-
}
121-
else
122-
{
123-
eof = true;
124-
return null;
125-
}
126-
}
127-
128-
public boolean eof()
129-
{
130-
return eof;
131-
}
132-
133-
public GXProperty next()
134-
{
135-
lastElement ++;
136-
if (count() > lastElement)
137-
{
138-
return vector.elementAt(lastElement);
139-
}
140-
else
141-
{
142-
eof = true;
143-
return null;
144-
}
145-
}
146-
147-
public Object GetJSONObject()
148-
{
67+
public int count() {
68+
return properties.size();
69+
}
70+
71+
public void clear() {
72+
properties.clear();
73+
}
74+
75+
public GXProperty first() {
76+
eof = false;
77+
if (count() > 0) {
78+
lastElement = 0;
79+
return properties.entrySet().iterator().next().getValue();
80+
} else {
81+
eof = true;
82+
return null;
83+
}
84+
}
85+
86+
public boolean eof() {
87+
return eof;
88+
}
89+
90+
public GXProperty next() {
91+
lastElement++;
92+
if (count() > lastElement) {
93+
return item(lastElement);
94+
} else {
95+
eof = true;
96+
return null;
97+
}
98+
}
99+
100+
public Object GetJSONObject() {
149101
JSONObject jObj = new JSONObject();
150102
int i = 0;
151-
while (count() > i)
152-
{
103+
while (count() > i) {
153104
GXProperty prop = item(i);
154105
try {
155-
jObj.put(prop.getKey(), prop.getValue());
156-
} catch (JSONException e) {
157-
}
106+
jObj.put(prop.getKey(), prop.getValue());
107+
} catch (JSONException e) {}
158108
i++;
159109
}
160-
return jObj;
110+
return jObj;
161111
}
162-
163-
public String toJSonString()
164-
{
165-
JSONObject jObj = (JSONObject)GetJSONObject();
112+
113+
public String toJSonString() {
114+
JSONObject jObj = (JSONObject) GetJSONObject();
166115
return jObj.toString();
167116
}
168-
public boolean fromJSonString(String s)
169-
{
117+
public boolean fromJSonString(String s) {
170118
return fromJSonString(s, null);
171-
}
172-
public boolean fromJSonString(String s, GXBaseCollection<SdtMessages_Message> messages)
173-
{
174-
this.clear();
119+
}
120+
public boolean fromJSonString(String s, GXBaseCollection < SdtMessages_Message > messages) {
121+
this.clear();
175122
if (!s.equals("")) {
176123
try {
177124
JSONObject jObj = new JSONObject(s);
178-
Iterator<String> keys = jObj.keys();
179-
while( keys.hasNext() ) {
125+
Iterator < String > keys = jObj.keys();
126+
while (keys.hasNext()) {
180127
String key = keys.next();
181128
this.put(key, jObj.get(key).toString());
182129
}
183130
return true;
184-
}
185-
catch (JSONException ex)
186-
{
131+
} catch (JSONException ex) {
187132
CommonUtil.ErrorToMessages("fromjson error", ex.getMessage(), messages);
188133
return false;
189134
}
190-
}
191-
else
192-
{
135+
} else {
193136
CommonUtil.ErrorToMessages("fromjson error", "empty string", messages);
194137
return false;
195138
}
196139
}
197-
}
140+
}

common/src/main/java/com/genexus/util/GXProperty.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.genexus.util;
22

3-
public class GXProperty
4-
{
3+
public class GXProperty {
54
public String name;
65
public String value;
6+
7+
public GXProperty() {}
8+
9+
public GXProperty(String name, String value){
10+
this.name = name;
11+
this.value = value;
12+
}
713

814
public String getKey()
915
{
@@ -23,6 +29,5 @@ public void setKey(String name)
2329
public void setValue(String value)
2430
{
2531
this.value = value;
26-
}
27-
28-
}
32+
}
33+
}

0 commit comments

Comments
 (0)