Skip to content

Commit bfe6ded

Browse files
rostamclaude
andcommitted
Modernize Java code: remove legacy APIs and improve type safety
- Replace Vector with ArrayList in GraphSaveObject - Replace deprecated javax.xml.bind.DatatypeConverter with java.util.Base64 - Replace raw Enumeration/Class types with typed generics in ExtensionClassLoader - Add try-with-resources for FileInputStream and ZipFile in ExtensionClassLoader - Replace string concatenation in loops with StringBuilder in AlgorithmUtils - Simplify isConnected() to use stream().distinct().count() - Fix raw ArrayX types to ArrayX<?> across preferences classes (AbstractPreference, GraphPreferences, UserDefinedEligiblity, ArrowHandler) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 414c9e3 commit bfe6ded

7 files changed

Lines changed: 84 additions & 87 deletions

File tree

src/graphtea/extensions/AlgorithmUtils.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ public static void resetVertexMarks(GraphModel g) {
5050
public static boolean isConnected(GraphModel g) {
5151
ArrayList<Integer> vs = new ArrayList<>();
5252
int[] parent = new int[g.getVerticesCount()];
53-
for(int i=0;i < g.getVerticesCount();i++) parent[i] = -1;
53+
for (int i = 0; i < g.getVerticesCount(); i++) {
54+
parent[i] = -1;
55+
}
5456
dfs(g, 0, vs, parent);
55-
return Arrays.stream(vs.toArray()).distinct().toArray().length == g.getVerticesCount();
57+
return vs.stream().distinct().count() == g.getVerticesCount();
5658
}
5759

5860
/**
@@ -428,17 +430,19 @@ public static String getEigenValues(GraphModel g) {
428430
EigenvalueDecomposition ed = A.eig();
429431
double[] rv = ed.getRealEigenvalues();
430432
double[] iv = ed.getImagEigenvalues();
431-
String res = "";
433+
StringBuilder res = new StringBuilder();
432434
for (int i = 0; i < rv.length; i++) {
433-
if (iv[i] != 0)
434-
res +="" + AlgorithmUtils.round(rv[i],10) + " + " + AlgorithmUtils.round(iv[i],10) + "i";
435-
else
436-
res += "" + AlgorithmUtils.round(rv[i],10);
437-
if(i!=rv.length-1) {
438-
res += ",";
435+
if (iv[i] != 0) {
436+
res.append(AlgorithmUtils.round(rv[i], 10)).append(" + ")
437+
.append(AlgorithmUtils.round(iv[i], 10)).append("i");
438+
} else {
439+
res.append(AlgorithmUtils.round(rv[i], 10));
440+
}
441+
if (i != rv.length - 1) {
442+
res.append(",");
439443
}
440444
}
441-
return res;
445+
return res.toString();
442446
}
443447

444448
/**
@@ -495,25 +499,27 @@ public static String getEigenValues(Matrix A) {
495499
EigenvalueDecomposition ed = A.eig();
496500
double[] rv = ed.getRealEigenvalues();
497501
double[] iv = ed.getImagEigenvalues();
498-
String res = "";
499-
List<Double> EigenValues = new ArrayList<>();
502+
StringBuilder res = new StringBuilder();
503+
List<Double> eigenValues = new ArrayList<>();
500504
for (int i = 0; i < rv.length; i++) {
501-
if (iv[i] != 0)
502-
res +="" + AlgorithmUtils.round(rv[i],10) + " + " + AlgorithmUtils.round(iv[i],10) + "i";
503-
else
504-
EigenValues.add(AlgorithmUtils.round(rv[i],10));
505-
}
506-
if(EigenValues.size() > 0) {
507-
res = "";
508-
EigenValues.sort((aDouble, t1) -> -aDouble.compareTo(t1));
509-
for (int i = 0; i < EigenValues.size(); i++) {
510-
res += EigenValues.get(i);
511-
if(i != EigenValues.size() - 1) {
512-
res+=",";
505+
if (iv[i] != 0) {
506+
res.append(AlgorithmUtils.round(rv[i], 10)).append(" + ")
507+
.append(AlgorithmUtils.round(iv[i], 10)).append("i");
508+
} else {
509+
eigenValues.add(AlgorithmUtils.round(rv[i], 10));
510+
}
511+
}
512+
if (!eigenValues.isEmpty()) {
513+
res = new StringBuilder();
514+
eigenValues.sort((a, b) -> -a.compareTo(b));
515+
for (int i = 0; i < eigenValues.size(); i++) {
516+
res.append(eigenValues.get(i));
517+
if (i != eigenValues.size() - 1) {
518+
res.append(",");
513519
}
514520
}
515521
}
516-
return res;
522+
return res.toString();
517523
}
518524

519525
// get kth minimum degree

src/graphtea/extensions/io/GraphSaveObject.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import graphtea.graph.graph.GraphModel;
66
import graphtea.graph.graph.Vertex;
77

8-
import javax.xml.bind.DatatypeConverter;
98
import java.io.*;
10-
import java.util.Vector;
9+
import java.util.ArrayList;
10+
import java.util.Base64;
1111

1212
/**
1313
* Created by rostam on 18.12.15.
@@ -22,8 +22,8 @@ public boolean equals(Object obj) {
2222
} else return false;
2323
}
2424

25-
public Vector<VertexSaveObject> vs = new Vector<>();
26-
Vector<EdgeSaveObject> es = new Vector<>();
25+
public ArrayList<VertexSaveObject> vs = new ArrayList<>();
26+
ArrayList<EdgeSaveObject> es = new ArrayList<>();
2727
boolean directed = false;
2828
String label = "";
2929
public GraphSaveObject(GraphModel g) {
@@ -71,15 +71,15 @@ public static byte[] getBytesOfGraphSaveObject(GraphSaveObject gso) {
7171
}
7272

7373
public static String graph2String(GraphModel g){
74-
return DatatypeConverter.printBase64Binary(getBytesOfGraph(g));
74+
return Base64.getMimeEncoder().encodeToString(getBytesOfGraph(g));
7575
}
7676

7777
public static GraphModel String2Graph(String s){
78-
return getGraphFromBytes(DatatypeConverter.parseBase64Binary(s));
78+
return getGraphFromBytes(Base64.getMimeDecoder().decode(s));
7979
}
8080

8181
public static GraphSaveObject string2GraphSaveObject(String s){
82-
return getGraphSaveOobjectfromBytes(DatatypeConverter.parseBase64Binary(s));
82+
return getGraphSaveOobjectfromBytes(Base64.getMimeDecoder().decode(s));
8383
}
8484

8585
public static GraphSaveObject getGraphSaveOobjectfromBytes(byte[] b){

src/graphtea/graph/old/ArrowHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public GraphPreferences GraphPrefFactory() {
106106
return new GraphPreferences(this.getClass().getSimpleName(), this, "Graph Drawings");
107107
}
108108

109-
public HashMap<Object, ArrayX> defineEligibleValuesForSettings(HashMap<Object, ArrayX> objectValues) {
110-
ArrayX t = new ArrayX(3);
109+
public HashMap<Object, ArrayX<?>> defineEligibleValuesForSettings(HashMap<Object, ArrayX<?>> objectValues) {
110+
ArrayX<Integer> t = new ArrayX<>(3);
111111
t.addValidValue(10);
112112
t.addValidValue(20);
113113
t.addValidValue(30);

src/graphtea/platform/extension/ExtensionClassLoader.java

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
55
package graphtea.platform.extension;
66

7-
import java.util.ArrayList;
8-
import java.util.List;
7+
import java.util.ArrayList;
8+
import java.util.List;
99
import graphtea.platform.StaticUtils;
1010
import graphtea.platform.core.exception.ExceptionHandler;
1111

@@ -22,7 +22,7 @@
2222
*/
2323
public class ExtensionClassLoader extends ClassLoader {
2424
public Map<String, byte[]> classesData = new HashMap<>();
25-
Map<String, Class> classes = new HashMap<>();
25+
Map<String, Class<?>> classes = new HashMap<>();
2626
List<File> unknownFiles = new ArrayList<>();
2727
public static URLClassLoader classLoader;
2828
public static ClassLoader cl;
@@ -31,7 +31,7 @@ public ExtensionClassLoader(String dirPath) {
3131
loadClasses(dirPath);
3232
}
3333

34-
public Collection<Class> getLoadedClasses() {
34+
public Collection<Class<?>> getLoadedClasses() {
3535
return classes.values();
3636
}
3737

@@ -60,14 +60,16 @@ private void loadClassFiles(String dir, String pack) {
6060
else if (file1.getName().endsWith(".class"))
6161
try {
6262
String name;
63-
if (pack.length() > 0)
63+
if (pack.length() > 0) {
6464
name = pack.substring(1) + ".";
65-
else
65+
} else {
6666
name = "";
67+
}
6768
name = name + file1.getName().substring(0, file1.getName().length() - 6);
6869
byte[] data = new byte[(int) file1.length()];
69-
FileInputStream fis = new FileInputStream(file1);
70-
fis.read(data);
70+
try (FileInputStream fis = new FileInputStream(file1)) {
71+
fis.read(data);
72+
}
7173
classesData.put(name, data);
7274
urls.add(file1.toURI().toURL());
7375
} catch (IOException e) {
@@ -85,26 +87,28 @@ private void loadClasses(String dirPath) {
8587
// defineClasses();
8688
}
8789

88-
public Class findClass(String name) throws ClassNotFoundException {
89-
Class ret = null;
90-
if (!classesData.containsKey(name))
90+
public Class<?> findClass(String name) throws ClassNotFoundException {
91+
Class<?> ret = null;
92+
if (!classesData.containsKey(name)) {
9193
ret = getParent().loadClass(name);
94+
}
9295
if (ret == null && !classes.containsKey(name)) {
9396
byte[] data = classesData.get(name);
94-
Class c = defineClass(name, data, 0, data.length);
97+
Class<?> c = defineClass(name, data, 0, data.length);
9598
classes.put(name, c);
9699
}
97100
ret = classes.get(name);
98-
if (ret == null)
101+
if (ret == null) {
99102
return classLoader.loadClass(name);
100-
else
103+
} else {
101104
return ret;
105+
}
102106
}
103107

104-
public Collection<Class> getClassesImplementing(Class cl) {
105-
Collection<Class> col = new ArrayList<>();
106-
for (Map.Entry<String, Class> entry1 : classes.entrySet()) {
107-
Class c = entry1.getValue();
108+
public Collection<Class<?>> getClassesImplementing(Class<?> cl) {
109+
Collection<Class<?>> col = new ArrayList<>();
110+
for (Map.Entry<String, Class<?>> entry1 : classes.entrySet()) {
111+
Class<?> c = entry1.getValue();
108112
if (StaticUtils.isImplementing(c, cl))
109113
col.add(c);
110114
}
@@ -131,36 +135,23 @@ public static void copyInputStream(InputStream in, OutputStream out)
131135
* @param zipFileName The given zipped file name
132136
*/
133137
public static void unZip(String zipFileName, String destDir) {
134-
System.out.println(zipFileName);
135-
Enumeration entries;
136-
ZipFile zipFile;
137-
138-
try {
139-
zipFile = new ZipFile(zipFileName);
140-
141-
entries = zipFile.entries();
142-
143-
while(entries.hasMoreElements()) {
144-
ZipEntry entry = (ZipEntry)entries.nextElement();
145-
146-
if(entry.isDirectory()) {
147-
// Assume directories are stored parents first then children.
148-
// System.err.println("Extracting directory: " + entry.getName());
149-
// This is not robust, just for demonstration purposes.
150-
(new File(destDir+File.separator+entry.getName())).mkdirs();
151-
continue;
152-
}
153-
154-
System.out.println("Extracting file: " + destDir+entry.getName());
155-
copyInputStream(zipFile.getInputStream(entry),
156-
new BufferedOutputStream(new FileOutputStream(destDir+File.separator+entry.getName())));
138+
try (ZipFile zipFile = new ZipFile(zipFileName)) {
139+
Enumeration<? extends ZipEntry> entries = zipFile.entries();
140+
while (entries.hasMoreElements()) {
141+
ZipEntry entry = entries.nextElement();
142+
if (entry.isDirectory()) {
143+
new File(destDir + File.separator + entry.getName()).mkdirs();
144+
continue;
145+
}
146+
try (InputStream in = zipFile.getInputStream(entry);
147+
OutputStream out = new BufferedOutputStream(
148+
new FileOutputStream(destDir + File.separator + entry.getName()))) {
149+
copyInputStream(in, out);
150+
}
151+
}
152+
} catch (IOException ioe) {
153+
ExceptionHandler.catchException(ioe);
157154
}
158-
159-
zipFile.close();
160-
} catch (IOException ioe) {
161-
System.err.println("Unhandled exception:");
162-
ExceptionHandler.catchException(ioe);
163-
}
164155
}
165156

166157

src/graphtea/platform/preferences/AbstractPreference.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public AbstractPreference(String name, Preferences pref, String category) {
3030

3131
public NotifiableAttributeSetImpl attributeSet = new NotifiableAttributeSetImpl();
3232

33-
protected void putAttribute(String name, ArrayX values) {
33+
protected void putAttribute(String name, ArrayX<?> values) {
3434
attributeSet.put(name, values);
3535
}
3636

@@ -45,7 +45,7 @@ protected <T> T getAttribute(String name) {
4545
}
4646

4747

48-
public abstract void defineAttributes(HashMap<Object, ArrayX> objectValues);
48+
public abstract void defineAttributes(HashMap<Object, ArrayX<?>> objectValues);
4949

5050

5151
public abstract void defineListeners(AttributeListener al);

src/graphtea/platform/preferences/GraphPreferences.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public GraphPreferences(String name, HashSet<Object> oneInstances, String catego
3535
defineListeners(this);
3636
}
3737

38-
public void defineAttributes(HashMap<Object, ArrayX> objectValues) {
38+
public void defineAttributes(HashMap<Object, ArrayX<?>> objectValues) {
3939

4040
for (Object o : objectValues.keySet()) {
4141
putAttribute(o.toString(), objectValues.get(o));
@@ -50,10 +50,10 @@ public void defineAttributes(HashMap<Object, Object> objectValues, boolean t) {
5050
}
5151
}
5252

53-
public void defineMultipleAttributes(HashMap<Object, HashMap<Object, ArrayX>> map) {
53+
public void defineMultipleAttributes(HashMap<Object, HashMap<Object, ArrayX<?>>> map) {
5454

5555
for (Object o : map.keySet()) {
56-
HashMap<Object, ArrayX> hashMap = map.get(o);
56+
HashMap<Object, ArrayX<?>> hashMap = map.get(o);
5757
for (Object fields : hashMap.keySet()) {
5858
putAttribute(o.toString() + "*" + fields.toString(), hashMap.get(fields));
5959

src/graphtea/platform/preferences/UserDefinedEligiblity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
public interface UserDefinedEligiblity {
1515
GraphPreferences GraphPrefFactory();
1616

17-
HashMap<Object, ArrayX> defineEligibleValuesForSettings(HashMap<Object, ArrayX> objectValues);
17+
HashMap<Object, ArrayX<?>> defineEligibleValuesForSettings(HashMap<Object, ArrayX<?>> objectValues);
1818
}

0 commit comments

Comments
 (0)