I'm trying to realize a personal abstractMap. I have 2 types of map in my project, one <String , List<Box>> and one <String , Box> (box is a class for encase an item with his quantity). But when I try the put method of map, it's showing me "UnsupportedOperationException - if the put operation is not supported by this map"
This is the abstract class for map
public abstract class TamagotchiMap<X> extends AbstractMap<String, X> {
#Override
public abstract Set<Entry<String, X>> entrySet();
public abstract void attachCategories(Set<String> categories);
public abstract void addItemForCategory(String category, Box box);
public abstract String getCategory(Box box);
public Collection<String> getAllCategories() {
return this.keySet();
}
}
and there are the 2 classes that extends this class
1)
public class InventoryMainMap extends TamagotchiMap<Box> {
private final Set<Entry<String, Box>> entry = new HashSet<>();
#Override
public Set<Entry<String, Box>> entrySet() {
return entry;
}
#Override
public void attachCategories(final Set<String> categories) {
for (String category: categories) {
this.put(category, null);
}
}
/**
*
* #return main item for this category
* #param category is the category of item
*/
public Box getMainItem(final String category) {
return this.get(category);
}
#Override
public String getCategory(final Box box) {
for (String category: this.getAllCategories()) {
if (this.get(category).containsItem(box.getItem())) {
return category;
}
}
return null;
}
#Override
public void addItemForCategory(final String category, final Box box) {
if (this.containsKey(category)) {
this.replace(category, box);
}
}
}
2)
public class ItemContainerMap extends TamagotchiMap<List<Box>> {
private final Set<Entry<String, List<Box>>> entry = new HashSet<>();
#Override
public Set<Entry<String, List<Box>>> entrySet() {
return entry;
}
#Override
public String getCategory(final Box box) {
for (String category: this.getAllCategories()) {
for (Box boxIterator: this.get(category)) {
if (boxIterator.containsItem(box.getItem())) {
return category;
}
}
}
return null;
}
#Override
public void addItemForCategory(final String category, final Box box) {
if (this.containsKey(category)) {
if (!this.get(category).contains(box)) {
System.out.println("nuovo item");
this.get(category).add(box);
} else {
System.out.println("item esistente");
this.get(category).stream().filter(iterBox -> iterBox.equals(box)).forEach(iterBox -> iterBox.increaseQuantity());
}
} else {
throw new NoSuchElementException();
}
}
#Override
public void attachCategories(final Set<String> categories) {
for (String category: categories) {
this.put(category, new LinkedList<Box>());
}
}
}
When I do
class Test {
TamagotchiMap map = new ItemContainerMap();
map.put(string, box);
}
it makes me see the Exception that I have already said.
As if the put was not usable, do you know where I was wrong?
throwing an UnsupportedOperationException is the default implementation of put(K, V) you inherit from AbstractMap. You could either implement it yourself, or, have your TamagotchiMap extend a concrete Map class, such as HashMap instead of AbstractMap.
Related
I have a list of classes with extend a base class
public class Entity{
abstract String getTitle();
}
The child classes are
public class ChildEntityOne extends Entity{
public static final String TITLE= "ABCD";
#Override
public String getTitle() {
return TITLE;;
}
}
public class ChildEntityTwo extends Entity{
public static final String TITLE= "EFGH";
#Override
public String getTitle() {
return TITLE;;
}
}
public class ChildEntityThree extends Entity{
public static final String TITLE= "WXYZ";
#Override
public String getTitle() {
return TITLE;;
}
}
now i'm trying to pass a list of valid classes to a function
which creates an instance from one of the classes from list and returns it
List<?ClassesToChooseFrom?> list = new ArrayList()<>;
list.add(?ChildEntityOne?);
list.add(?ChildEntityTwo?);
Entity result = getInstantiatedClass(list,getKey(),getjsonData())
if(result instanceof ChildEntityOne){
//do something
}else if(result instanceof ChildEntityTwo){
//do somwthing
}
public ?InstantiatedClassObject? getInstantiatedClass(List<?ClassesToChooseFrom?> list,String key,String jsonData){
foreach(?Class? itemclass : list){
if(itemClass.getTitle().equals(key)){
return new GsonBuilder().create().fromJson(jsonData, itemClass);}
}
return null;
}
Ive tried
List<Class<? extends Entity>> classes = new ArrayList<>();
but unable to go further..
You got the beginning right: a list of subclasses of Entity is:
List<Class<? extends Entity>> list = new ArrayList<>();
list.add(ChildEntityOne.class);
list.add(ChildEntityTwo.class);
Then you just need to make getInstantiatedClass use the same types you pass to it:
public Entity getInstantiatedClass(List<Class<? extends Entity>> list, String key, String jsonData) {
for (Class<? extends Entity> itemclass : list) {
if (getTitle(itemClass).equals(key)) {
...
You could make that generic, if you don't want to/need to do anything special for the Entity class.
public <T> T getInstantiatedClass(List<Class<? extends T>> list, String key, String jsonData) {
for (Class<? extends T> itemclass : list) {
...
To extract the value of the static TITLE field from a child entity class you can use:
private String getTitle(Class<?> itemclass) {
try {
return (String) itemclass.getField("TITLE").get(null);
} catch (IllegalAccessException | NoSuchFieldException e) {
return "N/A";
}
}
For Syncing with some pice of Hardware, I have create a Datamodel containing a generic Class
DataField to store different Types of Objects in a HashMap.
HashMap myDictionary.
Now found out that if I store, a DataField into an DataField, it is possible to save an String inside the DataField.
Is there any way to prevent this from happening.
Here is my Code:
public class DataField<T> implements IDataField<T> {
//region Fields
private T _value;
private boolean _hasChanged;
private ChildObserable<T> _hasChangedObserverable;
//endregion
//region Methodes
public DataField(T val) throws IllegalValueException {
if(val.getClass() != _value.getClass()) throw new IllegalValueException();
_value =val;
_hasChanged=false;
_hasChangedObserverable=new ChildObserable<>();
}
public DataField(T val,Observer observer){
_value =val;
_hasChanged=false;
_hasChangedObserverable=new ChildObserable<>();
addChangeObserver(observer);
}
//region getters
#Override
public T getValue() {
return _value;
}
#Override
public boolean hasBeChanged() {
return _hasChanged;
}
//endregion
//region setters
#Override
public void setValue(T value) throws IllegalValueException {
if(value.getClass()!= _value.getClass()) throw new IllegalValueException();
if(value!=_value) {
_value = value;
_hasChanged=true;
_hasChangedObserverable.sendValue(_value);
}
}
#Override
public void clearChangedState() {
_hasChanged=false;
}
//endregion
//region observers
#Override
public void addChangeObserver(Observer observer) {
_hasChangedObserverable.addObserver(observer);
}
#Override
public void removeChangeObserver(Observer observer) {
_hasChangedObserverable.deleteObserver(observer);
}
//endregion
//endregion
}
and here is an Sample of Code what I mean:
DataField dataField = new DataField<Integer>(5);
dataField.setValue(true); // This can be prevented because of checking Class
DataField dataField = new DataField<String>(null);
dataField.setValue(5); // This can not be prevented because of NullPointer Exception
I found some Solutions like this How to keep generic type of nested generics with class tokens
but I don't like to overgive the Class two times
Another Idea of mine was to use a default(T) Function like C# to presetMy Value to an specific Type.
Thanks for help
I solved it using a seconde Construtor and a Class Field.
public DataField(T val) throws IllegalValueException {
if(val==null) throw new IllegalArgumentException("Class type couldn't be resolved from null");
_value =val;
_class=val.getClass();
_hasChanged=false;
_hasChangedObserverable=new ChildObserable<>();
}
public DataField(T val,Class<T> classType) throws IllegalValueException {
if(val!=null && val.getClass()!=classType) throw new IllegalArgumentException("Type of val and classType are Different");
_class=classType;
_value=val;
_hasChanged=false;
_hasChangedObserverable=new ChildObserable<>();
}
#Override
public void setValue(T value) throws IllegalValueException {
if(_class!= value.getClass()) throw new IllegalValueException();
if(value!=_value) {
_value = value;
_hasChanged=true;
_hasChangedObserverable.sendValue(_value);
}
}
Thanks for help
I created a java project to apply my GraphTheory course and enhance my java skills.
In this project :
I created a class Sommet<S>(Vertex in English) with an attribute Id with a generic type called <S>.
I created a class Arc<S>(Edge in English) with two attributes Sommet(Vertex).
I created a class EnsembleArc which is an HashSet of Arc
I also created a class ArcValue which inherit from Arc and have an int attribute Valeur(Value in English)
Here everything is fine and I dont have any problem.
But then I created a class EnsembleArcValue which inherit from EnsembleArc because every method from EnsembleArc will be useful to EnsembleArcValue.
But I also want EnsembleArcValue to be an HashSet of ArcValue (and I dont want an Arc which is not an ArcValue). And with the inheritance EnsembleArcValue is able to have an "simple" Arc in his Set.
So my question after all this explanation is :
Is there a way for EnsembleArcValue to inherit from EnsembleArc but will only accept an ArcValue in his Set.
Here is an image of The UML Project
I hope it will help to understand my problem (dont look at the bottom).
Here is the code :
public class Sommet<S>
{
//attributes
private S id;
public Sommet(S s)
{
setId(s);
}
public S getId()
{
return id;
}
public void setId(S s)
{
assert s!= null: "Objet null passé en paramètre";
id = s;
}
#SuppressWarnings("unchecked")
#Override
public boolean equals(Object obj)
{
boolean callback;
if(obj.getClass()!=getClass())
{
callback=false;
}
else
{
if(((Sommet<S>)obj).getId().equals(getId()))
{
callback=true;
}
else
{
callback=false;
}
}
return callback;
}
#Override
public int hashCode()
{
return getId().hashCode();
}
#Override
public String toString()
{
return getId().toString();
}
}
public class Arc<S>
{
private Sommet<S> depart;
private Sommet<S> arrivee;
public Arc(Sommet<S> dep, Sommet<S> arr)
{
setDepart(dep);
setArrivee(arr);
}
#Override
public String toString()
{
String str="("+getDepart().getId()+","+getArrivee().getId()+")";
return str;
}
public Sommet<S> getDepart()
{
return depart;
}
public Sommet<S> getArrivee()
{
return arrivee;
}
public void setDepart(Sommet<S> depart)
{
this.depart = depart;
}
public void setArrivee(Sommet<S> arrivee)
{
this.arrivee = arrivee;
}
#SuppressWarnings("unchecked")
#Override
public boolean equals(Object obj)
{
boolean callback;
if(obj.getClass()!=getClass())
{
callback=false;
}
else
{
if(((Arc<S>)obj).getDepart().equals(getDepart())&&((Arc<S>)obj).getArrivee().equals(getArrivee()))
{
callback=true;
}
else
{
callback=false;
}
}
return callback;
}
#Override
public int hashCode()
{
return getArrivee().hashCode()+getDepart().hashCode();
}
}
public class ArcValue<S,V> extends Arc<S>
{
private V valeur;
public ArcValue (Sommet<S> depart, Sommet<S> arrivee, V valeur)
{
super(arrivee,depart);
this.valeur=valeur;
}
public V getValeur()
{
return valeur;
}
}
import java.util.HashSet;
public class Ensemble<E> extends HashSet<E> implements Cloneable
{
private static final long serialVersionUID = -4354387895748449845L;
public Ensemble ()
{
super();
}
public Ensemble (Ensemble<E> ensemble)
{
for (E e : ensemble)
{
add(e);
}
}
public String toString()
{
StringBuffer str=new StringBuffer("{");
for(E e: this)
{
str=str.append(e.toString()+",");
}
str.setCharAt(str.length()-1, '}');
return str.toString();
}
#SuppressWarnings("unchecked")
#Override
public Ensemble<E> clone()
{
return (Ensemble<E>)super.clone();
}
}
public class EnsembleArc<S> extends Ensemble<Arc<S>>
{
public EnsembleArc(Ensemble<Arc<S>> ensemble)
{
super(ensemble);
}
public EnsembleArc()
{
super();
}
private static final long serialVersionUID = -4099925554493145279L;
public EnsembleSommet<S> listSucc(Sommet<S> sommet)
{
EnsembleSommet<S> XSucc=new EnsembleSommet<S>();
for (Arc<S> arc : this)
{
if (arc.getDepart()==sommet)
{
XSucc.add(arc.getArrivee());
}
}
return XSucc;
}
public EnsembleSommet<S> listPred(Sommet<S> sommet)
{
EnsembleSommet<S> XPred=new EnsembleSommet<S>();
for (Arc<S> arc : this)
{
if (arc.getArrivee()==sommet)
{
XPred.add(arc.getDepart());
}
}
return XPred;
}
public void add(Sommet<S> depart,Sommet<S>arrivee)
{
add(new Arc<S>(depart,arrivee));
}
#Override
public EnsembleArc<S> clone ()
{
return (EnsembleArc<S>)super.clone();
}
}
//import java.util.Collection;
public class EnsembleArcValues<S,V> extends EnsembleArc<S> //implements Collection<ArcValue<S,V>>
{
//TODO faire en sorte que ensembleArcValués ne contienne que des ArcsValue
private static final long serialVersionUID = -7163498825360866323L;
}
And you'll need this one to :
public class EnsembleSommet<S> extends Ensemble<Sommet<S>>
{
public EnsembleSommet()
{
super();
}
public EnsembleSommet(EnsembleSommet<S> ensemble)
{
super(ensemble);
}
private static final long serialVersionUID = 7278825382690341067L;
#Override
public EnsembleSommet<S> clone ()
{
return (EnsembleSommet<S>)super.clone();
}
public Sommet<S> firstSommet()
{
#SuppressWarnings("unchecked")
Sommet<S>[] tab=new Sommet[size()];
return toArray(tab)[0];
}
}
The only way you can achieve this is to make the type of Arc you want part of your generic deceleration. Rename your existing EnsembleArc to AbstractEnsembleArc and change it's generic decleration from < S > to < S, T extends Arc< S > > i.e.:
public abstract class AbstractEnsembleArc<S, T extends Arc<S>> extends Ensemble<T> {
// PUT ALL YOUR LOGIC CURRENTLY IN EnsembleArc HERE
}
Now create a new Class Called EnsembleArc and extend the new abstract class you've added, this new class will work identically to your existing EnsembleArc and class decleration should now look like:
public class EnsembleArc<S> extends AbstractEnsembleArc<S, Arc<S>> {
}
Finally have EnsembleArcValues extend the Abstract class instead of EnsembleArc so that you can declare that it should only accepts ArcValue and not simple Arc, do that like this:
public class EnsembleArcValues<S, V> extends AbstractEnsembleArc<S, ArcValue<S, V>> {
}
I can use an extractor (Callback<E, Observable[]> extractor) to make a ListProperty fire change events if one of its elements changed one of its properties (update event).
Update Change Event in ObservableList
Is there an equivalent for ObjectProperty<>? I have an SimpleObjectProperty which I want to fire events when properties of it's value (another bean type) change (update change events).
Sample code:
public class TestBean {
public static <T extends TestBean> Callback<T, Observable[]> extractor() {
return (final T o) -> new Observable[] { o.testPropertyProperty() };
}
private final StringProperty testProperty = new SimpleStringProperty();
public final StringProperty testPropertyProperty() {
return this.testProperty;
}
public final String getTestProperty() {
return this.testPropertyProperty().get();
}
public final void setTestProperty(final String testProperty) {
this.testPropertyProperty().set(testProperty);
}
}
public class SomeType {
/**
* How can I listen for changes of TestBean#testProperty?
*/
private final ObjectProperty<TestBean> property = new SimpleObjectProperty<>();
}
I want to receive change events if the value of SomeType#property changes, but also, if SomeType#property#testProperty changes.
I cannot just listen for SomeType#property#testProperty, since I would not be notified when SomeType#property was changed (I would then listen on the wrong object for changes).
I want to receive change events if value of SomeType#property changes, but also, if SomeType#property#testProperty changes.
I cannot just listen for SomeType#property#testProperty, since I would not be notified, when SomeType#property was changed (I would then listen on the wrong object for changes).
This is a limitation of sorts of the current iteration of JavaFX. The built-in way is unreliable and you're better off using 3rd party libraries. See this answer for more information.
For you case, ReactFX can be utilized in a similar way:
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import org.reactfx.value.Val;
import org.reactfx.value.Var;
class TestBean {
private final StringProperty testProperty = new SimpleStringProperty();
public final StringProperty testPropertyProperty() { return testProperty; }
public final String getTestProperty() { return testProperty.get(); }
public final void setTestProperty(String newTestProperty) { testProperty.set(newTestProperty); }
}
public class SomeType {
private final ObjectProperty<TestBean> property = new SimpleObjectProperty<>();
public final ObjectProperty<TestBean> propertyProperty() { return property; }
public final TestBean getProperty() { return property.get(); }
public final void setProperty(TestBean newProperty) { property.set(newProperty); }
public static void main(String[] args) {
SomeType someType = new SomeType();
Var<String> chainedTestProperty = Val.selectVar(someType.propertyProperty(), TestBean::testPropertyProperty);
chainedTestProperty.addListener((obs, oldVal, newVal) -> System.out.println(obs + " " + oldVal + "->" + newVal));
//Tests
someType.setProperty(new TestBean());
someType.getProperty().setTestProperty("s1");
TestBean bean2 = new TestBean();
bean2.setTestProperty("s2");
someType.setProperty(bean2);
someType.setProperty(new TestBean());
}
}
Output:
org.reactfx.value.FlatMappedVar#7aec35a null->s1
org.reactfx.value.FlatMappedVar#7aec35a s1->s2
org.reactfx.value.FlatMappedVar#7aec35a s2->null
The key line
Var<String> chainedTestProperty = Val.selectVar(someType.propertyProperty(), TestBean::testPropertyProperty);
is a sort of listener chaining. The first argument is a property (OvservableValue) of some type Type. The second argument is the "sub"-property of some other type Type2 inside Type, which is given as a function from Type to that property.
Now whenever any "links" in the chain change, you are notified. You can continue to listen to changes in sub-sub-... properties by continuously chaining ovservables this way.
I came up with the following:
public class ObservableValueProperty<T> extends SimpleObjectProperty<T> {
private InvalidationListener listener = null;
private final Callback<T, Observable[]> extractor;
public ObservableValueProperty() {
this(null);
}
public ObservableValueProperty(final Callback<T, Observable[]> extractor) {
this.extractor = extractor;
}
#Override
protected void fireValueChangedEvent() {
super.fireValueChangedEvent();
}
#Override
public void setValue(final T v) {
if (extractor != null) {
final T oldValue = super.get();
if (oldValue != null) {
for (final Observable o : extractor.call(oldValue)) {
o.removeListener(listener);
}
}
listener = o -> fireValueChangedEvent();
for (final Observable o : extractor.call(v)) {
o.addListener(listener);
}
}
super.setValue(v);
}
}
public class ObservableValuePropertyTest4 implements ChangeListener<Object> {
#BeforeClass
public static void setUpBeforeClass() throws Exception {
}
#AfterClass
public static void tearDownAfterClass() throws Exception {
}
#Before
public void setUp() throws Exception {
}
#After
public void tearDown() throws Exception {
}
static class NestedBean {
StringProperty nestedProperty = new SimpleStringProperty("hans");
public static <T extends NestedBean> Callback<T, Observable[]> extractor() {
return (final T o) -> new Observable[] { o.nestedProperty };
}
#Override
public boolean equals(final Object obj) {
if (obj instanceof NestedBean) {
System.err.println(this.nestedProperty.get() + " " + ((NestedBean) obj).nestedProperty.get());
return Objects.equal(this.nestedProperty.get(), ((NestedBean) obj).nestedProperty.get());
}
return false;
}
}
private ObservableValueProperty<NestedBean> p;
private NestedBean nestedBean;
private String newNestedValue = null;
#Test
public void test01() {
p = new ObservableValueProperty<>(NestedBean.extractor());
nestedBean = new NestedBean();
p.setValue(nestedBean);
p.addListener(this);
nestedBean.nestedProperty.set("peter");
assertEquals("peter", newNestedValue);
}
#Override
public void changed(final ObservableValue<? extends Object> observable, final Object oldValue,
final Object newValue) {
System.err.println("Changed");
newNestedValue = nestedBean.nestedProperty.get();
}
}
Unfortunately, this does not fire any change events because of ExpressionHelper$SingleChange:
#Override
protected void fireValueChangedEvent() {
final T oldValue = currentValue;
currentValue = observable.getValue();
final boolean changed = (currentValue == null)? (oldValue != null) : !currentValue.equals(oldValue);
if (changed) {
try {
listener.changed(observable, oldValue, currentValue);
} catch (Exception e) {
Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e);
}
}
}
This checks for equality and only if not equal, notifies all listeners. When I trigger fireValueChangedEvent() the value has already changed, and new- and old values are equal, therefore no notification to listeners.
I had the same problem last week, and after many tries, I found a solution that seems to work as expected:
I created a new class called ObjectXProperty<E>, that has the same interface of an ObjectProperty<E>;
It has constructors that can accept a Callback<E,Observable[]>, our extractor function;
Inside the ObjectXProperty, I use a SimpleObjectProperty that deleguates all methods;
The magic trick lies in the set(E value) methods : I create an ObjectBinding that simply send back the value, but it uses the extractor function to decide when it's become invalidated!
This trick will not be applied if the bind method was used previously on the ObjectXProperty, to let the "real" binding do his job; it will work again if the unbind method is called;
Here's my new class ObjectXProperty<E> :
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.util.Callback;
/**
*
* #author Claude Bouchard - 2017
*/
public class ObjectXProperty<E> extends ObjectProperty<E> {
SimpleObjectProperty<E> p;
Callback<E, Observable[]> extractor;
boolean externalBound = false;
public ObjectXProperty(Callback<E, Observable[]> extractor) {
this.extractor = extractor;
}
public ObjectXProperty(E init, Callback<E, Observable[]> extractor) {
p = new SimpleObjectProperty();
this.extractor = extractor;
set(init);
}
public ObjectXProperty(Object bean, String name, Callback<E, Observable[]> extractor) {
p = new SimpleObjectProperty(bean, name);
this.extractor = extractor;
}
public ObjectXProperty(Object bean, String name, E init, Callback<E, Observable[]> extractor) {
p = new SimpleObjectProperty(bean, name);
this.extractor = extractor;
set(init);
}
#Override
public void set(E value) {
if (!externalBound) {
if (value != null) {
p.bind(Bindings.createObjectBinding(() -> {
return value;
}, extractor.call(value)));
} else {
p.bind(Bindings.createObjectBinding(() -> {
return value;
}, new Observable[]{}));
}
} else {
p.set(value); //As expected, it will throw a java.lang.RuntimeException
}
}
#Override
public E get() {
return p.get();
}
#Override
public void addListener(ChangeListener<? super E> listener) {
p.addListener(listener);
}
#Override
public void removeListener(ChangeListener<? super E> listener) {
p.removeListener(listener);
}
#Override
public void addListener(InvalidationListener listener) {
p.addListener(listener);
}
#Override
public void removeListener(InvalidationListener listener) {
p.removeListener(listener);
}
#Override
public Object getBean() {
return p.getBean();
}
#Override
public String getName() {
return p.getName();
}
#Override
public void bind(ObservableValue<? extends E> observable) {
p.bind(observable);
externalBound = true;
}
#Override
public void unbind() {
p.unbind();
externalBound = false;
set(get()); //to reactivate the extractor on the last value
}
#Override
public boolean isBound() {
return externalBound;
}
}
I think you need to add a listener to your object. This can be done simply. First of all you should write your class with a constructor and with getters this way:
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
public class SomeType {
public ObjectProperty<TestProperty> property;
public SomeType(TestProperty testProperty) {
this.property = new SimpleObjectProperty<>(testProperty);
}
public TestProperty getProperty() {
return property.get();
}
public ObjectProperty<TestProperty> propertyProperty() {
return property;
}
}
Then anywhere you have an instance of SomeType you can chain the properties, so you get the property the property's testProperty() and then simply add a listener to it.
someType.getProperty().testProperty().addListener((observable, oldValue, newValue) -> {
// Do whatever you want if the its value changed.
// You can also use its old or new value.
});
What is the best way of manipulating the order things are done based on some conditions (other than writing them again with the different order)?
Let's say there is a Person class and each object of Person represents a different human.
class Person{
int eatingPriority = 3;
int sleepingPriority = 2;
int recreationPriority = 1;
void eat() {/*eats*/}
void sleep() {/*sleeps*/}
void watchTv() {/*watches tv*/}
void satisfyNeeds() {
//HOW TO DO THIS
}
}
How can I make the satisfyNeeds() methods call the other three methods based on their priority?
Note: I want to make it clear that priorities can change from Person to Person.
You can do this with 1 class and 1 interface.
public class Person {
int eatingPriority = 3;
int sleepingPriority = 2;
int recreationPriority = 1;
PriorityQueue<Action> actions;
void eat() { }
void sleep() { }
void watchTv() { }
public Person() {
actions = new PriorityQueue<Action>(new Comparator<Action>() {
#Override
public int compare(Action o1, Action o2) {
return o2.getPriority() - o1.getPriority();
}
});
actions.add(new Action() {
#Override
public int getPriority() {
return eatingPriority;
}
#Override
public void execute() {
eat();
}
});
actions.add(new Action() {
#Override
public int getPriority() {
return sleepingPriority;
}
#Override
public void execute() {
sleep();
}
});
actions.add(new Action() {
#Override
public int getPriority() {
return recreationPriority;
}
#Override
public void execute() {
watchTv();
}
});
}
public void satisfyNeeds() {
for (Action action : actions) {
action.execute();
}
}
interface Action {
public int getPriority();
public void execute();
}
}
Here is another possible implementation :
abstract class Need {
abstract void satisfy();
}
class Eat extends Need {
#Override
public void satisfy() { /* eat ...*/}
}
class Sleep extends Need {
#Override
public void satisfy() { /* sleep ...*/}
}
class DrinkBeer extends Need {
#Override
public void satisfy() { /* drink beer ...*/}
}
class Person{
// TreeMap will sort the map in the key's natural order (a int here)
private Map<Integer, Need> needs = new TreeMap<>();
Person() {
add(new Eat(), 3);
add(new Sleep(), 2);
add(new DrinkBeer(), 1);
}
void add(Need need, int priority) {
needs.put(Integer.valueOf(priority), need);
}
void satisfyNeeds() {
for(Need need : needs.values())
need.satisfy();
}
}
This solution would require Java 8:
class Person {
void eat() {};
void sleep() {};
void watchTv() {};
// Being in a List you can easily reorder the needs when you want to
List<Runnable> needs = Arrays.asList(this::eat, this::sleep);
// Alternatively, you can use a Map<Runnable, Integer> where the value is your
// priority and sort it (see http://stackoverflow.com/q/109383/1296402)
void satisfyNeeds() {
needs.forEach(Runnable::run);
}
}
You can use this code
import java.util.Arrays; // must be imported
int[] priorities = {sleepPriority, eatPriority, recreationPriority};
Arrays.sort(priorities);
for (int i=priorities.length-1; 0<=i; i--) {
int priority = priorities[i];
if (priority == sleepingPriority) { sleep(); }
if (priority == eatingPriority) { eat(); }
if (priority == recreationPriority) { watchTv(); }
}
Basically, it puts the priorities in an array, sorts the array and runs a for loop on it to run the functions.
Finding the right order of three elements can be done simply like this:
void satisfyNeeds() {
boolean eatFirst = eatingPriority>Math.max(sleepingPriority,recreationPriority);
if(eatFirst) eat();
if(sleepingPriority>recreationPriority) {
sleep();
watchTv();
}
else {
watchTv();
sleep();
}
if(!eatFirst) eat();
}
Of course, it won’t scale if you raise the number of actions. For a higher number you might look at one of the other answers.
You should introduce a map property into Person class, where prioritize methods, for example:
class Person {
...
private Map<Integer, Method> methodsPriority = new HashMap<>();
...
public Person setEatingPriority(int priority) {
methodsPriority.put(priority, /* put 'eat' method reference here*/);
return this;
}
public Person setSleepingPriority(int priority) {
methodsPriority.put(priority, /* put 'sleep' method reference here*/);
return this;
}
public Person setWatchingTVPriority(int priority) {
methodsPriority.put(priority, /* put 'watch TV' method reference here*/);
return this;
}
public void satisfyNeeds() {
Collection<Integer> keys = methodsPriority.keySet();
Collections.sort(keys);
for(Integer key: keys)
methodsPriority.get(key).invoke(this);
}
...
}
And it can be used in next manner:
Person Anna = new Person()
.setEatingPriority(1)
.setSleepingPriority(2)
.setWatchingTVPriority(3);
Person Bob = new Person()
.setEatingPriority(3)
.setSleepingPriority(2)
.setWatchingTVPriority(1);
Anna.satisfyNeeds();
Bob.satisfyNeeds();