Corrections de bugs multiples

- Correction de quelques NPE
- Ajout d'une classe BiMap
- Correctif mineure dans le code
This commit is contained in:
Marc Baloup 2016-10-16 19:22:44 +02:00
parent 58e48bf27a
commit d9d5e77105
4 changed files with 51 additions and 4 deletions

View File

@ -0,0 +1,47 @@
package fr.pandacube.java.util;
import java.util.HashMap;
public class BiMap<K,V> {
HashMap<K, V> map = new HashMap<>();
HashMap<V, K> inversedMap = new HashMap<>();
public synchronized void put(K k, V v) {
if (map.containsKey(k) || inversedMap.containsKey(v)) {
map.remove(k);
inversedMap.remove(v);
}
map.put(k, v);
inversedMap.put(v, k);
}
public synchronized V get(K k) {
return map.get(k);
}
public synchronized K getKey(V v) {
return inversedMap.get(v);
}
public synchronized boolean containsKey(K k) {
return map.containsKey(k);
}
public synchronized boolean containsValue(V v) {
return inversedMap.containsKey(v);
}
public synchronized V remove(K k) {
V v = map.remove(k);
inversedMap.remove(v);
return v;
}
public synchronized K removeValue(V v) {
K k = inversedMap.remove(v);
map.remove(k);
return k;
}
}

View File

@ -38,7 +38,6 @@ public class ServerPropertyFile {
public boolean loadFromFile() {
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
@SuppressWarnings("unchecked")
Map<String, Object> dataFile = new Gson().fromJson(in, Map.class);
if (!dataFile.containsKey("name") || !(dataFile.get("name") instanceof String)) return false;

View File

@ -50,13 +50,14 @@ public class AbstractCommand {
* @return
*/
public static List<String> getTabProposalFromToken(String token, Collection<String> allProposal) {
List<String> ret = new ArrayList<String>();
List<String> ret = new ArrayList<>();
for (String s : allProposal)
if (s.toLowerCase().startsWith(token.toLowerCase())) ret.add(s);
if (s != null && s.toLowerCase().startsWith(token.toLowerCase())) ret.add(s);
if (ret.isEmpty()) ret.addAll(allProposal);
ret.removeIf(s -> s == null);
ret.sort(null); // String implents Comparable
return ret;

View File

@ -294,7 +294,7 @@ public class TCPServer extends Thread implements Closeable {
}
}
} catch (InterruptedException e) {} catch (IOException e) {}
} catch (InterruptedException|IOException e) {}
close();
}