Device-dependent image descriptor fields In a Java class that belongs in a library, I have a field size that indicates size of image. It must be int, but some values are depending to devices, so other developers can set  various types for it:


thumbnail (value depends on device)
real size (value depends on device) 
custom (arbitrary integer)


I want to restrict other developers (that use this library) to set value of size with one option from set of specific and meaningful options.My purpose is to show list of legal options to developer and also type safety, like Enum. But I think it is impossible to do that only by enum, so I create an interface and some classes:

public interface SizeMode {
}

public enum DevicePreDefinedImageSizeMode implements SizeMode {
    THUMBNAIL, REAL_SIZE
}

public enum CustomImageSizeMode implements SizeMode {
    CUSTOM_SIZE
}





public abstract class Size {

    private final SizeMode mode;

    public SizeMode getMode() {
        return mode;
    }

    public abstract int getDownSampleFactor();

    protected Size(SizeMode mode) {
        this.mode = mode;
    }
}

public class DevicePreDefinedImageSize extends Size {

    public DevicePreDefinedImageSize(DevicePreDefinedImageSizeMode mode) {
        super(mode);
    }

    @Override
    public int getDownSampleFactor() {
        throw new UnknownError("????");
    }

}

public class CustomImageSize extends Size {
    private final int downSampleFactor;

    private CustomImageSize(CustomImageSizeMode mode, int downSampleFactor) {
        super(mode);
        this.downSampleFactor = downSampleFactor;
    }

    @Override
    public int getDownSampleFactor() {
        return downSampleFactor;
    }

}


Now I can declare field size of type Size class and other developers are restricted to use one of sub classes of Size and initialize them with THUMBNAIL, REAL_SIZE or CUSTOM_SIZE.

But is there a better approach to achieve my purpose?
I'm afraid, it's hard to use. To create a size, I need to do something like

new DevicePreDefinedImageSize(
    DevicePreDefinedImageSizeMode.REAL_SIZE,
    number)


I don't think I could ever remember this. What about:

Size.newRealSize(number)


This can be done easily, all you need are three static methods. You can otherwise keep you classes as they are.



@Override
public int getDownSampleFactor() {
    throw new UnknownError("????");
}


This is a real pain. I can see that there's no downSampleFactor, but throwing four question marks isn't really helpful. I'd return 0. Though it may hide errors, it can make it easier to use.

Or, when you're defining so many classes, what about making them so that the unwanted method doesn't really exist? Declare it only where it makes sense.



Personally I wouldn't do six classes for this.