Hello all, been a while since I've asked a question. I'm working on a project just to practice and for fun and I've run into a snag.
I'm working on a painting type program and I'm trying to get a generic Buffer class for the images so I can use it for single float values like an alpha channel, or for a whole java.awt.Color. I'm setting up an interface for what the buffers will hold so that in any case I could call buffer.setPixel(x,y,buffer.getPixel(x,y).add(new ColorValue());
This is the interface I would like for BufferValue<T>:
/*
* Copyright © 2012 William Peckham
*/
package paint.buffers;
/**
* A base class for values that can be put into a paint.buffers.Buffer.
* @author William Matrix Peckham
*/
public interface BufferValue<T> extends Comparable<T>{
/**
* Add a value to this one
* @param second value to add
* @return result of addition
*/
public T add(T second);
/**
* Subtract a value from this one
* @param second value to subtract
* @return result of the subtraction
*/
public T sub(T second);
/**
* Multiply a value with this one
* @param second value to be multiplied
* @return result of the multiplication
*/
public T mul(T second);
/**
* Divide this by another value
* @param second value to divide by
* @return result of the division
*/
public T div(T second);
/**
* get a normalized value of this one based on the minimum and maximums
* @param min minimum
* @param max maximum
* @return normalized result
*/
public T normalize(T min, T max);
/**
* Return a value interpolated between first and second based on a.
* @param a interpolation parameter
* @param first first value to interpolate between
* @param second second value to interpolate between
* @return the intermediate value
*/
//WON'T COMPILE
public static T lerp(double a, T first, T second);
}
The buffer class definition looks like this: public class Buffer<T extends BufferValue>{....}
Then to implement floats would look like this:
public class FloatValue implements BufferValue<Float>{
float value;
public FloatValue(float f){
value=f;
}
public Float add(Float second){
return value+second;
}
public Float sub(Float second)//TRIVIAL IMPLEMENTATION
public Float mul(Float second)//TRIVIAL IMPLEMENTATION
public Float div(Float second)//TRIVIAL IMPLEMENTATION
public Float normalize(Float min, Float max){
return ((value-min)/(max-min))+min;
}
//WON'T COMPILE
public static Float lerp(double a, Float first, Float second){
return (1-a)*first+a*second;
}
}
NOTE: implemented hastely and probably wrong but should give the idea
I know that the last method is not possible for a number of reasons, static methods are not allowed in interfaces and if they were generic types cannot be used in static methods. but the reason that lerp can't be made with the other functions is because a java.awt.Color implementaiton couldn't must multiply the double a
...
Can someone suggest an alternative to this method that might work? I have a feeling theres a better way that I'm missing.
Off-Topic: I miss operator overloading when I work in java, it would solve this problem outright