The title can easily be misunderstood, but it boils down to that I most likely require a design-pattern to eliminate redundant code. To make my question as clear as possible I made a code example instead of writing a vague explanation.
Basically, I have the following functions:
getValue1(), getValue2(), getValue3(), ... , getValue12()
These functions could look as follows (though all differ slightly from each other and are not editable by means of making them implement an interface for a strategy pattern):
public int getValue1()
{
return 1 + 2;
}
Next we have a secondary class myClass which requires the values returned by the getValue() functions. A function from myClass would then look as follows (each differ in the fact that they make use of a different getValue() function):
public int getMyValues1()
{
int[] values = new int[10];
for (int i = 0; i < 10; i++) {
int[i] = getValue1() // NOTE: getValueX() may output differently each time.
}
}
We have arrived at our problem.
If we would make a getMyValues() function for each respective getValue(), we would have to copy and then paste the same code several times.
This goes against everything OOP languages stand for - that's why I require your help.
Any suggestion is much appreciated!
EDIT:
I reopened the question, because I didn't have Java 8 supported on the IDE I am to use.
Basically I have the following setup:
getValueClass
getValue1()
getValue2()
etc.
myClass
getMyValues1()
getMyValues2()
etc.
implemetingClass
private myClass mc = new MyClass()
main()
getLowestValue(int[] values)
And so main() could look as follows - if i'd wish to output the lowest value:
public static void main(String[] args)
{
...
System.out.print(getLowestValue(mc.getMyValues1()));
...
}
This edit goes to show that a strategy pattern isn't viable, since I have my functions in one class.
Hopefully this clears up any confusion and I really hope you guys can help me solve this issue!
Assuming these methods are public, then in Java 8, you should be able to use a functional interface (in this trivial example it would be a java.util.function.IntSupplier) and pass a reference to these methods as a lambda expression.
Something like (compilation not tested):
public int[] getMyValues(IntSupplier supplier) {
int[] values = new int[10];
for (int i = 0; i < 10; i++) {
int[i] = supplier.getAsInt()
}
return values;
}
Called using:
int[] values = someobject.getMyValues(someobject::getValues1)
This is more or less using those methods as strategies without the need to make an actual interface and multiple implementations: the strategy interface is the functional interface itself and the method references generate the implementation.
EDIT: if you can't use Java 8, then you can just define your own interface with just 1 method that returns the int. The calling just becomes longer because of the lack of support for method references:
int[] values = someobject.getMyValues(new MyIntProducer() {
public int getValue() { return someobject.getValues1(); };
}
Note that the someobject local variable will need to be made final for this to work.
You can use reflection. I don't recommend doing it this way but there is a time place for this type of thing.
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class ReflectiveGetter {
private final Object theObject;
private final String methodPattern;
private final Map<Integer, Method> methodsByIndex = new HashMap<Integer, Method>();
public ReflectiveGetter(Object theObject, String methodPattern) {
this.theObject = theObject;
this.methodPattern = methodPattern;
String patternToMatch = methodPattern + "\\d+";
for(Method m : theObject.getClass().getMethods()) {
String name = m.getName();
if(name.matches(patternToMatch)) {
m.setAccessible(true);
int i = Integer.parseInt(name.substring(methodPattern.length()));
methodsByIndex.put(i, m);
}
}
}
public int getValue(int index)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Method m = methodsByIndex.get(index);
if(m != null) {
return (Integer)m.invoke(theObject);
}
throw new NoSuchMethodException(methodPattern + index);
}
}
Usage is:
ReflectiveGetter rg = new ReflectiveGetter(theValueObject, "getValue");
System.out.println(rg.getValue(1)); // prints as if theValueObject.getValue1()
Reflection is clumsy and you should not use it if you do not know what you are doing or there are more convenient options.
For example, perhaps the value class should be using a Map to begin with.
This example is taken from Thinking in Java.
public class Automobile {
}
public class Holder<T> {
private T a;
public Holder(T a){
this.a = a;
}
public void set(T a){
this.a = a;
}
public T get(){
return a;
}
public static void main(String[] args){
Holder<Automobile> h = new Holder<Automobile>(new Automobile());
Automobile a = (Automobile)h.get();
}
}
Then there goes explanation: you must specify what type you want to put into it using the same angle brackets syntax as you can see in main().
Well, I can't understand anything. I would understand that must word as a possible compile time error in case of violation of this rule.
But this works:
Holder<Automobile> h = new Holder(new Automobile());
Automobile a = h.get();
And this works:
Holder h = new Holder(new Automobile());
Automobile a = (Automobile)h.get();
So, as I can see, the compiler won't control what I put into the Holder object. Well, then I don't catch generics at all. And I have two questions:
What is the reason to use them? Only to save me some effort when casting object back to Automobile?
Is there any way to make the compiler control me so that I should really put Automobile into the Holder?
The casting here is unnecessary:
Holder<Automobile> h = new Holder<Automobile>(new Automobile());
Automobile a = (Automobile)h.get();
But here it is necessary:
Holder h = new Holder(new Automobile());
Automobile a = (Automobile)h.get();
And this is the best way of doing things ever since java 1.5 and above:
Holder<Automobile> h = new Holder<Automobile>(new Automobile()); //specify the generic parameter on both static and dynamic type
Automobile a = h.get(); //no casting is necessary
Or above java 1.7 for simplicity:
Holder<Automobile> h = new Holder<>(new Automobile()); //diamond operator so you don't need to respecify the same thing
Automobile a = h.get();
The reason why it's useful to use generics in this fashion is so that you can't do the following:
Integer a = new Integer(6);
List list = new ArrayList();
list.add(a);
list.add("5");
for(int i = 0; i < list.size(); i++)
{
Integer integer = (Integer)list.get(i); //crashes at "5" which is String at runtime
System.out.println(integer);
}
As you can see, if you can put any subclass of Object into the list without bounds, then you need explicit casting, and if you put anything that is not what you expect it to be into the List, then it will crash. Please note that without generics, you're also not told what type you are expected to place into the list, which means you need to keep track of what the List is supposed to contain, which is really crappy when you're trying to do logic like the following: Java generics and casting to a primitive type
And I'm not even sure if this is possible without generics: Is it possible to cast Map<Field, Value> to Map<Mirror, Mirror> when it is known that Field and Value extend Mirror?
So technically generics enable extra features while also encourages type safety and therefore error-less code, which is always nice.
Integer a = new Integer(6);
List<Integer> list = new ArrayList<Integer>();
list.add(a);
list.add("5"); //this will not compile -> you won't need to wait until runtime to see that things are incorrect!
The point of generics is to write datastructures which can contain or work with any object. You specify at use-time which types of objects it should contain, e.g.:
ArrayList<String> stringList = new ArrayList<String>();
(Diamondoperator for show not written in short)
to answer the questions:
1: to write code which can work with all objects (like the collections), and yes, the compiler understands and can type-check your code at compiletime
2: yeah, dont use generics and instead write the code spezific for your automobiles
This question already has answers here:
What's the reason I can't create generic array types in Java?
(16 answers)
Closed 9 years ago.
I have a generic K and wish to create an empty array so I type
private K[] keys = new K[0];
However eclipse says "Cannot create an generic array of K"
Have I missed something? You can create an empty array of int, is this something generics can't do?
You cannot create arrays of generics in Java. A workaround would be:
keys = (K[])new Object[size];
Although this will generate a compiler warning.
Generally, this is not a good practice and it is not safe to do it. See this thread for more.
However, if you just can't avoid it, it should only be used with new as this casting of an Object array to K[] might be used to insert stuff you won't like.
Update:
But really, as you can see in this answer java.lang.reflect.Array.newInstance() is probably the safer way using the reflection library.
What if K is abstract? How would the executor know how to fill any missing methods?
Also, due to type-erasure upon compilation, you can't tell what class K actually is without comparing it to others, which would require an instance.
Type erasure
You can use the Array.newInstance() method to achieve something like this; consider
public class Question<K> {
private K[] keys;
#SuppressWarnings("unchecked")
public Question(Class<? extends K> cls, int size) {
this.keys = (K[]) Array.newInstance(cls, size);
}
public K[] getKeys() {
return keys;
}
public static void main(String[] args) {
Question<String> question = new Question<String>(
String.class, 10);
String[] keys = question.getKeys();
keys[0] = "Hello";
keys[1] = "World";
keys[2] = "Goodbye";
keys[3] = "I must";
keys[4] = "be Going";
keys[5] = "Now";
keys[6] = "Parting";
keys[7] = "is";
keys[8] = "such";
keys[9] = "sweet sorrow";
System.out.println(Arrays.toString(question
.getKeys()));
}
}
Here this prints
[Hello, World, Goodbye, I must, be Going, Now, Parting, is, such, sweet sorrow]
How can I check to make sure my variable is an int, array, double, etc...?
Edit: For example, how can I check that a variable is an array? Is there some function to do this?
Java is a statically typed language, so the compiler does most of this checking for you. Once you declare a variable to be a certain type, the compiler will ensure that it is only ever assigned values of that type (or values that are sub-types of that type).
The examples you gave (int, array, double) these are all primitives, and there are no sub-types of them. Thus, if you declare a variable to be an int:
int x;
You can be sure it will only ever hold int values.
If you declared a variable to be a List, however, it is possible that the variable will hold sub-types of List. Examples of these include ArrayList, LinkedList, etc.
If you did have a List variable, and you needed to know if it was an ArrayList, you could do the following:
List y;
...
if (y instanceof ArrayList) {
...its and ArrayList...
}
However, if you find yourself thinking you need to do that, you may want to rethink your approach. In most cases, if you follow object-oriented principles, you will not need to do this. There are, of course, exceptions to every rule, though.
Actually quite easy to roll your own tester, by abusing Java's method overload ability. Though I'm still curious if there is an official method in the sdk.
Example:
class Typetester {
void printType(byte x) {
System.out.println(x + " is an byte");
}
void printType(int x) {
System.out.println(x + " is an int");
}
void printType(float x) {
System.out.println(x + " is an float");
}
void printType(double x) {
System.out.println(x + " is an double");
}
void printType(char x) {
System.out.println(x + " is an char");
}
}
then:
Typetester t = new Typetester();
t.printType( yourVariable );
a.getClass().getName() - will give you the datatype of the actual object referred to by a, but not the datatype that the variable a was originally declared as or subsequently cast to.
boolean b = a instanceof String - will give you whether or not the actual object referred to by a is an instance of a specific class.
Again, the datatype that the variable a was originally declared as or subsequently cast to has no bearing on the result of the instanceof operator.
I took this information from:
How do you know a variable type in java?
This can happen. I'm trying to parse a String into an int and I'd like to know if my Integer.parseInt(s.substring(a, b)) is kicking out an int or garbage before I try to sum it up.
By the way, this is known as Reflection. Here's some more information on the subject: http://docs.oracle.com/javase/tutorial/reflect/
Just use:
.getClass().getSimpleName();
Example:
StringBuilder randSB = new StringBuilder("just a String");
System.out.println(randSB.getClass().getSimpleName());
Output:
StringBuilder
You may work with Integer instead of int, Double instead of double, etc. (such classes exists for all primitive types).
Then you may use the operator instanceof, like if(var instanceof Integer){...}
Well, I think checking the type of variable can be done this way.
public <T extends Object> void checkType(T object) {
if (object instanceof Integer)
System.out.println("Integer ");
else if(object instanceof Double)
System.out.println("Double ");
else if(object instanceof Float)
System.out.println("Float : ");
else if(object instanceof List)
System.out.println("List! ");
else if(object instanceof Set)
System.out.println("Set! ");
}
This way you need not have multiple overloaded methods. I think it is good practice to use collections over arrays due to the added benefits. Having said that, I do not know how to check for an array type. Maybe someone can improve this solution. Hope this helps!
P.S Yes, I know that this doesn't check for primitives as well.
The first part of your question is meaningless. There is no circumstance in which you don't know the type of a primitive variable at compile time.
Re the second part, the only circumstance that you don't already know whether a variable is an array is if it is an Object. In which case object.getClass().isArray() will tell you.
I did it using: if(x.getClass() == MyClass.class){...}
I wasn't happy with any of these answers, and the one that's right has no explanation and negative votes so I searched around, found some stuff and edited it so that it is easy to understand. Have a play with it, not as straight forward as one would hope.
//move your variable into an Object type
Object obj=whatYouAreChecking;
System.out.println(obj);
// moving the class type into a Class variable
Class cls=obj.getClass();
System.out.println(cls);
// convert that Class Variable to a neat String
String answer = cls.getSimpleName();
System.out.println(answer);
Here is a method:
public static void checkClass (Object obj) {
Class cls = obj.getClass();
System.out.println("The type of the object is: " + cls.getSimpleName());
}
Basically , For example :
public class Kerem
{
public static void main(String[] args)
{
short x = 10;
short y = 3;
Object o = y;
System.out.println(o.getClass()); // java.lang.Short
}
}
None of these answers work if the variable is an uninitialized generic type
And from what I can find, it's only possible using an extremely ugly workaround, or by passing in an initialized parameter to your function, making it in-place, see here:
<T> T MyMethod(...){ if(T.class == MyClass.class){...}}
Is NOT valid because you cannot pull the type out of the T parameter directly, since it is erased at runtime time.
<T> void MyMethod(T out, ...){ if(out.getClass() == MyClass.class){...}}
This works because the caller is responsible to instantiating the variable out before calling. This will still throw an exception if out is null when called, but compared to the linked solution, this is by far the easiest way to do this
I know this is a kind of specific application, but since this is the first result on google for finding the type of a variable with java (and given that T is a kind of variable), I feel it should be included
var.getClass().getSimpleName()
Let's take a example
String[] anArrayOfStrings = { "Agra", "Mysore", "Chandigarh", "Bhopal" };
List<String> strList = Arrays.asList(anArrayOfStrings);
anArrayOfStrings.getClass().getSimpleName() //res => String[]
strList.getClass().getSimpleName() // res => ArrayList
You can check it easily using Java.lang.Class.getSimpleName() Method Only if variable has non-primitive type. It doesnt work with primitive types int ,long etc.
reference - Here is the Oracle docs link
I hit this question as I was trying to get something similar working using Generics. Taking some of the answers and adding getClass().isArray() I get the following that seems to work.
public class TypeTester <T extends Number>{
<T extends Object> String tester(T ToTest){
if (ToTest instanceof Integer) return ("Integer");
else if(ToTest instanceof Double) return ("Double");
else if(ToTest instanceof Float) return ("Float");
else if(ToTest instanceof String) return ("String");
else if(ToTest.getClass().isArray()) return ("Array");
else return ("Unsure");
}
}
I call it with this where the myArray part was simply to get an Array into callFunction.tester() to test it.
public class Generics {
public static void main(String[] args) {
int [] myArray = new int [10];
TypeTester<Integer> callFunction = new TypeTester<Integer>();
System.out.println(callFunction.tester(myArray));
}
}
You can swap out the myArray in the final line for say 10.2F to test Float etc
public static void chkType(Object var){
String type = var.getClass().toString();
System.out.println(type.substring(16));
//assertEquals(type,"class java.lang.Boolean");
//assertEquals(type,"class java.lang.Character");
//assertEquals(type,"class java.lang.Integer");
//assertEquals(type,"class java.lang.Double");
}
A simple solution I found was the following rather than wondering about fire command. Also, you can check this article
public class DataTypeCheck
{
public static void main(String[] args)
{
String jobTitle = "Agent";
int employeeId = 7;
double floating= 10.0;
String bond = jobTitle + employeeId;
System.out.println(((Object)floating).getClass().getSimpleName());
System.out.println(((Object)employeeId).getClass().getSimpleName());
System.out.println(((Object)jobTitle).getClass().getSimpleName());
System.out.println(((Object)bond).getClass().getSimpleName());
}
}
Output:
Double
Integer
String
String
I am looking for a simple, concise way to convert a given Number object to an object of a given numeric type.
Loss of precision due to narrowing conversions is fine
I prefer not to go through strings.
I need something like:
private static Number convert(Number num, Class<? extends Number> targetType)
Is there a way to do it without checking all the combinations of types?
I think the clearest way is to use brute force:
private static Number convert(Number num, Class<? extends Number> targetType) {
Number result = null;
if (Byte.class.equals(targetType)) {
result = Byte.valueOf(num.byteValue());
} else if (Short.class.equals(targetType)) {
result = Short.valueOf(num.shortValue());
} else if (...) {
...
} else {
throw new IllegalArgumentException("targetType is not a Number");
}
return result;
}
You might use reflection, but I find that more hackish.
You can use something like:
String simpleName = targetType.getSimpleName().toLowerCase();
if (simpleName.equals("integer")) {
simpleName = "int";
}
Method m = number.getClass().getMethod(simpleName + "Value");
return (Number) m.invoke(number);
This relies on the fact that Number has methods like longValue(), floatValue(), etc.
As for BigInteger or AtomicInteger - you can use their constructor reflectively, which accepts one argument - the primitive type.
Perhaps converting via BigDecimal would work?