|
| 1 | +package com.example.app6; |
| 2 | + |
1 | 3 |
|
2 | 4 | /****************************** |
3 | | -* Copyright (c) 2003,2019 Kevin Lano |
| 5 | +* Copyright (c) 2003,2020 Kevin Lano |
4 | 6 | * This program and the accompanying materials are made available under the |
5 | 7 | * terms of the Eclipse Public License 2.0 which is available at |
6 | 8 | * http://www.eclipse.org/legal/epl-2.0 |
@@ -178,7 +180,42 @@ public static <T> ArrayList<T> initialiseSequence(T ... args) |
178 | 180 | { result.add(args[i]); } |
179 | 181 | return result; |
180 | 182 | } |
| 183 | + |
| 184 | + public static <T> copySet(Collection<T> s) |
| 185 | + { HashSet<T> result = new HashSet<T>(); |
| 186 | + result.addAll(s); |
| 187 | + return result; |
| 188 | + } |
| 189 | + |
| 190 | + public static <T> copySequence(Collection<T> s) |
| 191 | + { ArrayList<T> result = new ArrayList<T>(); |
| 192 | + result.addAll(s); |
| 193 | + return result; |
| 194 | + } |
| 195 | + |
| 196 | + public static <T extends Comparable> T min(Collection<T> s) |
| 197 | + { ArrayList<T> slist = new ArrayList<T>(); |
| 198 | + slist.addAll(s); |
| 199 | + T result = slist.get(0); |
| 200 | + for (int i = 1; i < slist.size(); i++) |
| 201 | + { T val = slist.get(i); |
| 202 | + if (val.compareTo(result) < 0) |
| 203 | + { result = val; } |
| 204 | + } |
| 205 | + return result; |
| 206 | + } |
181 | 207 |
|
| 208 | + public static <T extends Comparable> T max(Collection<T> s) |
| 209 | + { ArrayList<T> slist = new ArrayList<T>(); |
| 210 | + slist.addAll(s); |
| 211 | + T result = slist.get(0); |
| 212 | + for (int i = 1; i < slist.size(); i++) |
| 213 | + { T val = slist.get(i); |
| 214 | + if (0 < val.compareTo(result)) |
| 215 | + { result = val; } |
| 216 | + } |
| 217 | + return result; |
| 218 | + } |
182 | 219 |
|
183 | 220 |
|
184 | 221 | public static <T> HashSet<T> addSet(HashSet<T> s, T x) |
@@ -492,7 +529,7 @@ static <T> ArrayList<T> mergeSort(final ArrayList<T> a, java.util.Map<T,Comparab |
492 | 529 | } |
493 | 530 |
|
494 | 531 |
|
495 | | - public static ArrayList<Integer> integerSubrange(int i, int j) |
| 532 | + public static ArrayList<Integer> integerSubrange(int i, double j) |
496 | 533 | { ArrayList<Integer> tmp = new ArrayList<Integer>(); |
497 | 534 | for (int k = i; k <= j; k++) |
498 | 535 | { tmp.add(new Integer(k)); } |
@@ -906,5 +943,51 @@ public static <D,R> Map<D,R> addMap(Map<D,R> m, D key, R value) |
906 | 943 | { m.put(key,value); |
907 | 944 | return m; |
908 | 945 | } |
909 | | - |
| 946 | + |
| 947 | + public static ArrayList<String> tokeniseCSV(String line) |
| 948 | + { StringBuffer buff = new StringBuffer(); |
| 949 | + int x = 0; |
| 950 | + int len = line.length(); |
| 951 | + boolean instring = false; |
| 952 | + ArrayList<String> res = new ArrayList<String>(); |
| 953 | + while (x < len) |
| 954 | + { char chr = line.charAt(x); |
| 955 | + x++; |
| 956 | + if (chr == ',') |
| 957 | + { if (instring) { buff.append(chr); } |
| 958 | + else |
| 959 | + { res.add(buff.toString().trim()); |
| 960 | + buff = new StringBuffer(); |
| 961 | + } |
| 962 | + } |
| 963 | + else if ('"' == chr) |
| 964 | + { if (instring) { instring = false; } |
| 965 | + else { instring = true; } |
| 966 | + } |
| 967 | + else |
| 968 | + { buff.append(chr); } |
| 969 | + } |
| 970 | + res.add(buff.toString().trim()); |
| 971 | + return res; |
| 972 | + } |
| 973 | + |
| 974 | + public static ArrayList<String> parseCSVtable(String rows) |
| 975 | + { StringBuffer buff = new StringBuffer(); |
| 976 | + int x = 0; |
| 977 | + int len = rows.length(); |
| 978 | + boolean instring = false; |
| 979 | + ArrayList<String> res = new ArrayList<String>(); |
| 980 | + while (x < len) |
| 981 | + { char chr = rows.charAt(x); |
| 982 | + x++; |
| 983 | + if (chr == '\n') |
| 984 | + { res.add(buff.toString().trim()); |
| 985 | + buff = new StringBuffer(); |
| 986 | + } |
| 987 | + else |
| 988 | + { buff.append(chr); } |
| 989 | + } |
| 990 | + res.add(buff.toString().trim()); |
| 991 | + return res; |
| 992 | + } |
910 | 993 | } |
0 commit comments