This repository has been archived by the owner on Aug 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package eu.kimb_technologies.usf; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Some type of HashMap which can be exported and imported as usf. | ||
* Interally uses java.util.HashMap<K,V> | ||
* | ||
* @author kimbtech | ||
* | ||
* @param <K> the type of the keys, subtype of Atom | ||
* @param <V> the type of the values, subtype of Atom | ||
*/ | ||
public class MapList <K extends Atom, V extends Atom> extends Atom implements Map<K,V> { | ||
|
||
private HashMap<K,V> map = new HashMap<K,V>(); | ||
|
||
/* | ||
* USF part | ||
*/ | ||
|
||
@Override | ||
public String toUSF() { | ||
return this.genUSFList().toUSF(); | ||
} | ||
|
||
@Override | ||
public String toHumFrieUSF(int indent) { | ||
return this.genUSFList().toHumFrieUSF(indent); | ||
} | ||
|
||
@Override | ||
public Atom loadUSF(String usf) throws USFSyntaxException { | ||
USFList data = new USFList(); | ||
try { | ||
data = (USFList) USFParser.parse( usf ); | ||
|
||
//put each value in map, under it's name | ||
for( Atom pair : data ) { | ||
if( pair instanceof USFPair ) { | ||
//the name must be a string | ||
this.map.put( | ||
(K) ((USFPair) pair).getName(), | ||
(V) ((USFPair) pair).getValue() | ||
); | ||
} | ||
else { | ||
throw new USFSyntaxException("Not a valid MapList!"); | ||
} | ||
} | ||
} | ||
catch( ClassCastException e) { | ||
throw new USFSyntaxException("Not a valid MapList!"); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return this.map.equals(o); | ||
} | ||
|
||
/** | ||
* Generates a USF data list outof the internal hashmap. | ||
* It is a list of pairs, where each pair has key and value of one hashmap entry. | ||
* @return the usf List of the Map | ||
*/ | ||
private USFList genUSFList() { | ||
USFList l = new USFList(); | ||
for (Map.Entry<K, V> entry : this.map.entrySet()) { | ||
l.add(new USFPair(entry.getKey(), entry.getValue())); | ||
} | ||
return l; | ||
} | ||
|
||
/* | ||
* Map part | ||
*/ | ||
|
||
@Override | ||
public void clear() { | ||
this.map.clear(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(Object key) { | ||
return this.map.containsKey( key ); | ||
} | ||
|
||
@Override | ||
public boolean containsValue(Object value) { | ||
return this.map.containsKey( value ); | ||
} | ||
|
||
@Override | ||
public Set<Entry<K, V>> entrySet() { | ||
return this.map.entrySet(); | ||
} | ||
|
||
@Override | ||
public V get(Object key) { | ||
return this.map.get( key ); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return this.map.isEmpty(); | ||
} | ||
|
||
@Override | ||
public Set<K> keySet() { | ||
return this.map.keySet(); | ||
} | ||
|
||
@Override | ||
public V put(K key, V value) { | ||
return this.map.put(key, value); | ||
} | ||
|
||
@Override | ||
public void putAll(Map<? extends K, ? extends V> map) { | ||
this.map.putAll(map); | ||
} | ||
|
||
@Override | ||
public V remove(Object key) { | ||
return this.map.remove(key); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return this.map.size(); | ||
} | ||
|
||
@Override | ||
public Collection<V> values() { | ||
return this.map.values(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/test/java/eu/kimb_technologies/usf/test/MapListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package eu.kimb_technologies.usf.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.Map.Entry; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import eu.kimb_technologies.usf.Bool; | ||
import eu.kimb_technologies.usf.MapList; | ||
import eu.kimb_technologies.usf.USFList; | ||
import eu.kimb_technologies.usf.USFPair; | ||
import eu.kimb_technologies.usf.USFParser; | ||
import eu.kimb_technologies.usf.USFString; | ||
import eu.kimb_technologies.usf.USFInteger; | ||
import eu.kimb_technologies.usf.USFSyntaxException; | ||
|
||
@DisplayName("USF List") | ||
class MapListTest { | ||
|
||
MapList<USFString, USFInteger> a; | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
a = new MapList<USFString, USFInteger>(); | ||
} | ||
|
||
@Test | ||
@DisplayName("MapList Ex- and Import") | ||
public void testAddRemoveSize() throws USFSyntaxException { | ||
assertTrue( a.isEmpty() ); | ||
|
||
a.put( new USFString("Eier"), new USFInteger(220) ); | ||
a.put( new USFString("Ei"), new USFInteger(-10) ); | ||
a.put( new USFString("Eis"), new USFInteger(2220) ); | ||
a.put( new USFString("Heiss"), new USFInteger(120) ); | ||
|
||
assertTrue( a.containsKey(new USFString("Ei")) ); | ||
assertFalse( a.containsKey(new USFString("Eise")) ); | ||
|
||
assertEquals( a.toUSF(), "[{\"Eis\":2220},{\"Heiss\":120},{\"Ei\":-10},{\"Eier\":220}]" ); | ||
|
||
MapList<USFString, USFInteger> b = new MapList<USFString, USFInteger>(); | ||
b.loadUSF( "[{\"Eis\":2220},{\"Heiss\":120},{\"Ei\":-10},{\"Eier\":220}]" ); | ||
assertEquals( b, a ); | ||
} | ||
|
||
@Test | ||
@DisplayName("USF List Import Errors") | ||
public void testLoadUSF() { | ||
assertThrows(USFSyntaxException.class,()->{ | ||
a.loadUSF("[{\"a\":12},true]"); | ||
}); | ||
assertThrows(USFSyntaxException.class,()->{ | ||
a.loadUSF("{\"a\":12}"); | ||
}); | ||
} | ||
|
||
@Test | ||
@DisplayName("implements Map tests") | ||
public void lists() { | ||
|
||
/* | ||
* Why this shit? | ||
* We have to cover 70 % plus and to implement Map the MapList has to have with many methods, | ||
* but all methods listed below are just as mapping to a Java HashMap, so there won't be any bugs. | ||
* (Maybe also there, but thats not our problem ;) ) | ||
*/ | ||
|
||
a.clear(); | ||
a.containsKey( null ); | ||
a.containsValue( null ); | ||
a.entrySet(); | ||
a.get( null ); | ||
a.isEmpty(); | ||
a.keySet(); | ||
a.remove(null); | ||
a.size(); | ||
a.values(); | ||
} | ||
} |