How to allow two types of data as parameters
private void setSomething(final List<?> someListOfObjects){
}
EDIT
Sorry, I think I am confusing others-
I want the list to contain only Double and/or a custom object (a user defined class). It can be a mix of the two. How do I assure it?
EDITING WITH MORE DETAILS:
We generate simple excel files for different client programs using Apache POI. I am writing a template for that. To create a row, a user can pass a list of doubles as cell values for a row and the program uses the default style (CellStyle) for the cell. Also, a user can pass a list which can be consisted of doubles and a type of custom objects. The custom object is a class where the cell value a开发者_如何学Gond the style for that cell are defined. So, when I am creating a row, I get the values of the list. If the value is a double, I apply default cell style for that cell, but if the value is the custom object then I apply the given style.
I want the list to contain only Double and/or a custom object (a user defined class). It can be a mix of the two. How do I assure it?
That's not possible, and it's bad design because it breaks type safety - you cannot know what type the list contains at a given index.
We'd have to know more about what you're actually trying to do to say what would be the best design solution.
Edit: Instead of allowing a mixing of raw values and styled values ("custom objects") in your list, you should allow only homogenous lists and a convenient way to produce a "custom object" with the default style.
Create another class which wraps the Double
and/or CustomObject
and use this as list type instead.
public class Container {
private Double d;
private CustomObject customObject;
// ...
}
with
private void setSomething(List<Container> containers) {
// ...
}
Try List<Number>
which will allow all wrapper classes.
I don't think it's possible, you have to define two methods.
Alternatively, you could use:
private <T extends Number> void setSomething(final List<T> someListOfObjects)
I want the list to contain only Double and/or a custom object
As there is no generic or
the list would have the generic type Object making compile-time checks impossible and even if there was something like that you couldn't use the objects without casting them (ugly code).
You can split your list in two to get compile-time checks.
private void setSomething(List<Double> listDoubles, List<MyClass> listOther);
Or you have to use instanceOf at runtime
private void setSomething(List<?> listObj){
for(Object o:listObject){
if(!(o instanceof Double))
if(!(o instanceof MyClass))
throw new IllegalArgumentException();
}
}
"Double and/or a custom object" is ver different! And works like this:
private <T extends Double & CustomObject> void setSomething(final List<T> someListOfObjects)
But or is not possible with a real type check.
EDIT (after more details given):
What about two vararg methods?
private void setSomething(final Double... someListOfObjects)
private void setSomething(final CustomObject... someListOfObjects)
Instead of the usual List types (which are not usable here) you could create an own List-type which allows mixed objects. Here an idea for an interface:
public interface MixedList {
public int size();
/**
* returns an element of the list, if it has the right type.
* @return null if the element is not of the right type, otherwise
* the element.
*/
<T> public T get(int index, Class<T> type);
/**
* returns the type of an element.
*/
public Class<?> getType(int index);
/**
* adds a typed element to the end of the collection
*/
<T> public void add(Class<T> type, T element);
/**
* replaces an element of the collection.
*/
<T> public void set(int index Class<T> type, T element)
}
But as the other answerers already said, this would be a pain to program against, as you always had to check the type. Maybe a better solution would be to not store elements of Double and CustomClass, but only CustomClass, with you providing a simple function like
public CustomClass wrapNumber(double d)
to create unformatted cells.
Or simply use untyped collections (List or such) and look for every object whether it is one of the supported types. (To generate Spreadsheets, often text Strings would also be useful, I think.)
EDITED: Because contents is mixed:
private void setSomething(final List someListOfObjects)
{
for(Object n : someListOfObjects){
if(n instanceof Double) {}
else if(n instanceof Long) {}
...
}
}
精彩评论