From 81344cbc00d7db079f88f4250b081949defe406c Mon Sep 17 00:00:00 2001 From: tomas-sexenian Date: Wed, 6 Mar 2024 09:58:04 -0300 Subject: [PATCH 1/2] Use hashmap for storing GXProperties Issue:107267 --- .../java/com/genexus/util/GXProperties.java | 259 +++++++----------- .../java/com/genexus/util/GXProperty.java | 13 +- 2 files changed, 109 insertions(+), 163 deletions(-) diff --git a/common/src/main/java/com/genexus/util/GXProperties.java b/common/src/main/java/com/genexus/util/GXProperties.java index a04ee091f..09f8585c0 100644 --- a/common/src/main/java/com/genexus/util/GXProperties.java +++ b/common/src/main/java/com/genexus/util/GXProperties.java @@ -1,6 +1,6 @@ package com.genexus.util; -import java.util.Vector; +import java.util.LinkedHashMap; import com.genexus.internet.IGxJSONSerializable; @@ -10,188 +10,131 @@ import com.genexus.SdtMessages_Message; import com.genexus.GXBaseCollection; import java.util.Iterator; +import java.util.Map; -public class GXProperties implements IGxJSONSerializable{ - private Vector vector = new Vector(); - private boolean eof; - private int lastElement; - - public GXProperties() { - } - - public void set(String name, String value) - { - put(name, value); - } - - public void add(String name, String value) - { - addToTheEnd(name, value); - } - - public void put(String name, String value) - { - int index = findElement(name); - if ( index >= 0) - { - vector.elementAt(index).setValue(value); - } - else - { - addToTheEnd(name, value); - } - } - public String toString() { - StringBuilder builder = new StringBuilder(); - for (GXProperty property : vector) { - builder.append(property.getValue()); - } - return builder.toString(); - } - private void addToTheEnd(String name, String value){ - - GXProperty prop = new GXProperty(); - prop.setKey(name); - prop.setValue(value); - vector.addElement(prop); - } - public String get(String name) - { - int index = findElement(name); - if (index >= 0) - return vector.elementAt(index).getValue(); - else - return ""; - } - - public void remove(String name) - { - int index = findElement(name); - if (index >= 0) - vector.removeElementAt(index); - } - - public boolean containsKey(String name) - { - if (findElement(name) == -1) - return false; - return true; - } - - private int findElement(String name) - { - int i = 0; - while (count() > i) - { - if (item(i).getKey().equalsIgnoreCase(name)) - return i; - i++; - } - return -1; - } - - public GXProperty item(int i) - { - return vector.elementAt(i); - } - - public int getCount() - { +public class GXProperties implements IGxJSONSerializable { + private LinkedHashMap < String, GXProperty > properties = new LinkedHashMap < > (); + private boolean eof; + private int lastElement; + + public GXProperties() {} + + public void set(String name, String value) { + this.put(name, value); + } + + public void add(String name, String value) { + properties.put(name, new GXProperty(name, value)); + } + + public void put(String name, String value) { + properties.put(name, new GXProperty(name, value)); + } + public String toString() { + StringBuilder builder = new StringBuilder(); + for (GXProperty property: properties.values()) { + builder.append(property.getValue()); + } + return builder.toString(); + } + + public String get(String name) { + return containsKey(name) ? properties.get(name).getValue() : ""; + } + + public void remove(String name) { + properties.remove(name); + } + + public boolean containsKey(String name) { + return properties.containsKey(name); + } + + public GXProperty item(int i) { + int counter = 0; + for (Map.Entry < String, GXProperty > entry: properties.entrySet()) { + if (counter++ == i) { + return entry.getValue(); + } + } + throw new IndexOutOfBoundsException("The provided index is larger than the amount of items stored"); + } + + public int getCount() { return count(); } - public int count() - { - return vector.size(); - } - - public void clear() - { - vector.removeAllElements(); - } - - public GXProperty first() - { - eof = false; - if (count() > 0) - { - lastElement = 0; - return vector.elementAt(0); - } - else - { - eof = true; - return null; - } - } - - public boolean eof() - { - return eof; - } - - public GXProperty next() - { - lastElement ++; - if (count() > lastElement) - { - return vector.elementAt(lastElement); - } - else - { - eof = true; - return null; - } - } - - public Object GetJSONObject() - { + public int count() { + return properties.size(); + } + + public void clear() { + properties.clear(); + } + + public GXProperty first() { + eof = false; + if (count() > 0) { + lastElement = 0; + return properties.entrySet().iterator().next().getValue(); + } else { + eof = true; + return null; + } + } + + public boolean eof() { + return eof; + } + + public GXProperty next() { + lastElement++; + if (count() > lastElement) { + return item(lastElement); + } else { + eof = true; + return null; + } + } + + public Object GetJSONObject() { JSONObject jObj = new JSONObject(); int i = 0; - while (count() > i) - { + while (count() > i) { GXProperty prop = item(i); try { - jObj.put(prop.getKey(), prop.getValue()); - } catch (JSONException e) { - } + jObj.put(prop.getKey(), prop.getValue()); + } catch (JSONException e) {} i++; } - return jObj; + return jObj; } - - public String toJSonString() - { - JSONObject jObj = (JSONObject)GetJSONObject(); + + public String toJSonString() { + JSONObject jObj = (JSONObject) GetJSONObject(); return jObj.toString(); } - public boolean fromJSonString(String s) - { + public boolean fromJSonString(String s) { return fromJSonString(s, null); - } - public boolean fromJSonString(String s, GXBaseCollection messages) - { - this.clear(); + } + public boolean fromJSonString(String s, GXBaseCollection < SdtMessages_Message > messages) { + this.clear(); if (!s.equals("")) { try { JSONObject jObj = new JSONObject(s); - Iterator keys = jObj.keys(); - while( keys.hasNext() ) { + Iterator < String > keys = jObj.keys(); + while (keys.hasNext()) { String key = keys.next(); this.put(key, jObj.get(key).toString()); } return true; - } - catch (JSONException ex) - { + } catch (JSONException ex) { CommonUtil.ErrorToMessages("fromjson error", ex.getMessage(), messages); return false; } - } - else - { + } else { CommonUtil.ErrorToMessages("fromjson error", "empty string", messages); return false; } } -} +} \ No newline at end of file diff --git a/common/src/main/java/com/genexus/util/GXProperty.java b/common/src/main/java/com/genexus/util/GXProperty.java index a55b155ae..d7dcc2a4d 100644 --- a/common/src/main/java/com/genexus/util/GXProperty.java +++ b/common/src/main/java/com/genexus/util/GXProperty.java @@ -1,9 +1,13 @@ package com.genexus.util; -public class GXProperty -{ +public class GXProperty { public String name; public String value; + + public GXProperty(String name, String value){ + this.name = name; + this.value = value; + } public String getKey() { @@ -23,6 +27,5 @@ public void setKey(String name) public void setValue(String value) { this.value = value; - } - -} + } +} \ No newline at end of file From 5d8970bc9144b9933e5073ce4e1bd51bb3f77134 Mon Sep 17 00:00:00 2001 From: tomas-sexenian Date: Mon, 18 Mar 2024 12:43:25 -0300 Subject: [PATCH 2/2] Add no argument constructor --- common/src/main/java/com/genexus/util/GXProperty.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/src/main/java/com/genexus/util/GXProperty.java b/common/src/main/java/com/genexus/util/GXProperty.java index d7dcc2a4d..b747c91c4 100644 --- a/common/src/main/java/com/genexus/util/GXProperty.java +++ b/common/src/main/java/com/genexus/util/GXProperty.java @@ -4,6 +4,8 @@ public class GXProperty { public String name; public String value; + public GXProperty() {} + public GXProperty(String name, String value){ this.name = name; this.value = value;