JavaCAD
PropertyStorage.java
Go to the documentation of this file.
1 
34 package eu.mihosoft.vrl.v3d;
35 
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.Optional;
39 import javafx.scene.paint.Color;
40 
41 // TODO: Auto-generated Javadoc
47 public class PropertyStorage {
48 
50  private final Map<String, Object> map = new HashMap<>();
51 
53  private static final Color[] colors = {
54  Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA,
55  Color.WHITE, Color.BLACK, Color.GRAY, Color.ORANGE};
56 
60  public PropertyStorage() {
61  randomColor(this);
62  }
63 
70  public void set(String key, Object property) {
71  map.put(key, property);
72  }
73 
82  public <T> Optional<T> getValue(String key) {
83 
84  Object value = map.get(key);
85 
86  try {
87  return Optional.ofNullable((T) value);
88  } catch (ClassCastException ex) {
89  return Optional.empty();
90  }
91  }
92 
98  public void delete(String key) {
99  map.remove(key);
100  }
101 
109  public boolean contains(String key) {
110  return map.containsKey(key);
111  }
112 
118  static void randomColor(PropertyStorage storage) {
119  Color c = colors[(int) (Math.random() * colors.length)];
120 
121  storage.set("material:color",
122  "" + c.getRed()
123  + " " + c.getGreen()
124  + " " + c.getBlue());
125  }
126 }
final Map< String, Object > map
void set(String key, Object property)