snippetjavaMinor
Enum Constants for Convert Units
Viewed 0 times
unitsconvertenumforconstants
Problem
I have a few constants in a game that I'm doing for hobby. I need to store constants for the total size of the a Physical World and the size of the Screen.
I can position the world's objects to the screen or vice versa.
To position the images on the screen for example I get the position that the body occupies in the physical world and transform to the screen by doing:
I came up with an ingenious way to keep these constant while I do conversion operations using the enum below:
I can use it this way:
What do you think of this way of using constants and operations? Do you see any problems? Could you point out some improvement?
I can position the world's objects to the screen or vice versa.
To position the images on the screen for example I get the position that the body occupies in the physical world and transform to the screen by doing:
(Full Screen Size / Total size of the World) * position of the object in the world.I came up with an ingenious way to keep these constant while I do conversion operations using the enum below:
import com.badlogic.gdx.math.Vector2;
import static br.com.games.Test.Sizes.*;
public class Test {
public static enum Sizes {
WORLD(new Vector2(25.806f, 15.48f)), SCREEN(new Vector2(800f, 480f)), TRANSFORM(
new Vector2(-1, -1));
private Vector2 value;
private Sizes(Vector2 value) {
this.value = value;
}
private Vector2 inputValue;
private Vector2 sourceValue;
public Sizes value(Vector2 value) {
inputValue = value;
return this;
}
public Sizes from(Sizes size) {
sourceValue = size.value;
return this;
}
public Vector2 to(Sizes size) {
float x = size.value.x / sourceValue.x * inputValue.x;
float y = size.value.y / sourceValue.y * inputValue.y;
return Vector2.Zero.set(x, y);
}
public Vector2 getValue() {
return value;
}
}
}I can use it this way:
public static void main(String[] args) {
TRANSFORM.value(new Vector2(10, 10)).from(WORLD)
.to(SCREEN);
}Vector2 class is here.What do you think of this way of using constants and operations? Do you see any problems? Could you point out some improvement?
Solution
I'm not at all sold on
If you're really hankering for a fluent API for some reason, you could do
** EDIT **
Since you're interested in the fluent approach, here's something to get you started. Obviously it needs documentation and input checking. The DisplayType enum can either belong to Transform or it can be top-level, depending on your needs. Likewise, the two inner classes can easily be split out.
As far as not instantiating objects, you can't really do a fluent API correctly without new objects. Do you have a real, demonstrated performance issue with creating new objects? Many people wrongly assume that object creation and garbage collection will be a big issue without testing.
If you really need to do this without new object instances, then your only good option as I see it is static methods. Your Sizes enum is too easy to use incorrectly.
TRANSFORM being part of your enum. The API for this enum lets you make nonsense calls like WORLD.from(WORLD).to(TRANSFORM). Always try to prevent invalid use of your APIs. I'm not really sure why you wouldn't use a naive approach like two static methods Transformer.worldToScreen(Vector2) and Transformer.screenToWorld(Vector2). Are you anticipating needing additional conversions later?If you're really hankering for a fluent API for some reason, you could do
Transform.value(Vector2D).from(Sizes).to(Sizes). You can control the return types to prevent at least some of the possible errors, but there are still some invalid calls like Transform.value(...).from(WORLD).to(WORLD).** EDIT **
Since you're interested in the fluent approach, here's something to get you started. Obviously it needs documentation and input checking. The DisplayType enum can either belong to Transform or it can be top-level, depending on your needs. Likewise, the two inner classes can easily be split out.
As far as not instantiating objects, you can't really do a fluent API correctly without new objects. Do you have a real, demonstrated performance issue with creating new objects? Many people wrongly assume that object creation and garbage collection will be a big issue without testing.
If you really need to do this without new object instances, then your only good option as I see it is static methods. Your Sizes enum is too easy to use incorrectly.
public final class Transform {
public static enum DisplayType {
SCREEN(new Vector2(800f, 480f)),
WORLD(new Vector2(25.806f, 15.48f));
private final Vector2 size;
private DisplayType(final Vector2 size) {
this.size = size;
}
}
private Transform() {
super();
}
public static From value(final Vector2 value) {
return new From(value);
}
public static final class From {
private final Vector2 value;
From(final Vector2 value) {
this.value = value;
}
public To from(final DisplayType sourceDisplayType) {
return new To(this.value, sourceDisplayType);
}
}
public static final class To {
private final Vector2 value;
private final DisplayType from;
To(final Vector2 value, final DisplayType from) {
this.value = value;
this.from = from;
}
public Vector2 to(final DisplayType targetDisplayType) {
/* TODO: Do math here */
throw new UnsupportedOperationException();
}
}
}Code Snippets
public final class Transform {
public static enum DisplayType {
SCREEN(new Vector2(800f, 480f)),
WORLD(new Vector2(25.806f, 15.48f));
private final Vector2 size;
private DisplayType(final Vector2 size) {
this.size = size;
}
}
private Transform() {
super();
}
public static From value(final Vector2 value) {
return new From(value);
}
public static final class From {
private final Vector2 value;
From(final Vector2 value) {
this.value = value;
}
public To from(final DisplayType sourceDisplayType) {
return new To(this.value, sourceDisplayType);
}
}
public static final class To {
private final Vector2 value;
private final DisplayType from;
To(final Vector2 value, final DisplayType from) {
this.value = value;
this.from = from;
}
public Vector2 to(final DisplayType targetDisplayType) {
/* TODO: Do math here */
throw new UnsupportedOperationException();
}
}
}Context
StackExchange Code Review Q#88418, answer score: 6
Revisions (0)
No revisions yet.