peer-at-code-parser-java/src/dev/peerat/parser/Bag.java
2024-10-02 14:29:59 +02:00

68 lines
1.3 KiB
Java

package dev.peerat.parser;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class Bag{
private Map<String, Object> map;
private Object value;
private String path = "";
public Bag(){
this.map = new HashMap<>();
path = Thread.currentThread().getStackTrace()[2].toString();
}
public Bag(Bag bag){
this.map = new HashMap<>(bag.map);
this.value = bag.value;
this.path = bag.path;
}
public <E> E get(){
return (E) this.value;
}
public void addPath(String s){
path+=s;
}
public String path(){
return this.path;
}
public void set(Object value){
this.value = value;
System.out.println("edit Bag "+this);
}
public <E> E get(String key){
return (E) this.map.get(key);
}
public boolean has(String key){
return this.map.containsKey(key);
}
public void remove(String key){
this.map.remove(key);
}
public void set(String key, Object value){
this.map.put(key, value);
System.out.println("edit Bag "+this);
}
@Override
public String toString(){
String map = "";
for(Entry<String, Object> entry : this.map.entrySet()) map+=","+(entry.getKey())+" -> "+entry.getValue();
if(map.length() > 0) map = map.substring(1);
return "([bag] "+(value != null ? "| value="+value+" ":"")+"| map["+Arrays.toString(this.map.keySet().toArray())+"]="+map+" )";
}
}