patternjavaMinor
Detecting available List operations
Viewed 0 times
listoperationsdetectingavailable
Problem
I'm writing some code which needs to take a generic
I'm currently just calling the functions on the list and creating a set of available operations, but I'm wondering if there's a better way than
Current code:
Edit: This is supposed to be a JPanel that contains a list that can be modified by a program and this should reflect those changes on the list display. It's not complete yet, but the ultimate goal is to have a list that can be modified by the user or programmer. It will have some buttons that will be disabled if the list doesn't support the operations, so it might not have
java.util.List from another source and adapt to the available functions; eg. does it support add(), set(), etc.I'm currently just calling the functions on the list and creating a set of available operations, but I'm wondering if there's a better way than
try-catch.Current code:
import java.util.*;
import javax.swing.JPanel;
public class JListPanel extends JPanel {
private static final long serialVersionUID = 832057842976869638L;
private static enum JLPFlag {
ADD, REMOVE, SET;
}
private List list;
private Set flags = EnumSet.allOf(JLPFlag.class);
public JListPanel(List backinglist) {
list = backinglist;
setFlags();
}
private void setFlags() {
// store the last state
List prev = Collections.unmodifiableList(list);
boolean empty = list.isEmpty();
try {
if (!empty) {
list.add(list.get(0));
} else {
// hope that list allows null
list.add(null);
}
} catch (Exception e) {
flags.remove(JLPFlag.ADD);
}
try {
list.set(0, list.get(0));
} catch (Exception e) {
flags.remove(JLPFlag.SET);
}
try {
list.remove(0);
} catch (Exception e) {
flags.remove(JLPFlag.REMOVE);
}
if (flags.contains(JLPFlag.REMOVE))
list.removeAll(list);
if (flags.contains(JLPFlag.ADD))
list.addAll(prev);
}
}Edit: This is supposed to be a JPanel that contains a list that can be modified by a program and this should reflect those changes on the list display. It's not complete yet, but the ultimate goal is to have a list that can be modified by the user or programmer. It will have some buttons that will be disabled if the list doesn't support the operations, so it might not have
add or removeSolution
After edit original question:
I understand what you are trying to do, and very nice if you can do that.
That's why I'm trying to think together with you for an solution.
The only thing is that I seems not capable enough to reach to an solution.
That's why I'm still posting mine thoughts.
We have some problems(with solutions) that prevents us to check it at creation of the class :
This solves issue 3 but still not issue 2 when only add is supported we can't remove our T from the list. (reversed is not a problem cause it shall return
Still a problem if it throws the
Other bad solution :
Create an enum with all known implementations of List.
Check for instanceOf of the class and get the flags from the enum.
Why possible bad :
When I create a subclass of
Still the
I understand what you are trying to do, and very nice if you can do that.
That's why I'm trying to think together with you for an solution.
The only thing is that I seems not capable enough to reach to an solution.
That's why I'm still posting mine thoughts.
We have some problems(with solutions) that prevents us to check it at creation of the class :
- We init with a list, and after constructing the "wrapper" it must be the same list.
- The original list may support one or more methods, add may be supported and an addAll or remove maybe not,
- Adding/removing a null can be supported or not. So if we want to test add we need a instance of T.
- We can make the wrapper abstract and create an
abstract T getInstance ();where we give a real instance ofTor we disallow a wrapper with an emptyList.
This solves issue 3 but still not issue 2 when only add is supported we can't remove our T from the list. (reversed is not a problem cause it shall return
false if T isn't in the list)- Maybe we could use the Object
clone()what gives a shallow copy but that is in this case no problem as the copy is used to test the methods.
Still a problem if it throws the
CloneNotSupportedException.- We can at construction check if the
CloneNotSupportedExceptionis thrown and do the test at runtime => buttons are disabled when user press the button once or buttons are always disabled when cloning isn't possible.
Other bad solution :
Create an enum with all known implementations of List.
Check for instanceOf of the class and get the flags from the enum.
Why possible bad :
ArrayList implements everything.When I create a subclass of
ArrayList and throw at the add an UnsupportedOperationException your check for ArrayList is good and set the flags for an ArrayList.Still the
add operation shall not work.Context
StackExchange Code Review Q#48382, answer score: 4
Revisions (0)
No revisions yet.