Solved

Java circular enum constructors (catch 22)

Posted on 2014-11-28
3
174 Views
Last Modified: 2014-11-28
I have an enum for MeasurementTypes, that says a TEMPERATURE measurement uses units CELSIUS or FAHRENHEIT.

Then I have a Units class which defines CELSIUS and FAHRENHEIT as being TEMPERATURE measurements.

I create a measurement, get the measurement type, and discover in the constructor for MeasurementType the TEMPERATURE measurement type doesn't have the units CELSIUS and FAHRENHEIT filled in.

I suppose because TEMPERATURE needs CELSIUS and FAHRENHEIT to already exist and be initialized, while CELSIUS and FAHRENHEIT need TEMPERATURE to already exist and be initialized.

Well that's a fine mess of spaghetti I've gotten myself into now. Any suggestions how to get around this?

Below is some sample code:
public enum Units {
	
	CELSIUS(MeasurementType.TEMPERATURE),
	FAHRENHEIT(MeasurementType.TEMPERATURE);

	
	private final MeasurementType measurementType;

	//CONSTRUCTORS
	private Units(MeasurementType measurementType) {
		this.measurementType = measurementType;
	}
}

Open in new window

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public enum MeasurementType {
	TEMPERATURE(  new ArrayList<Units>(Arrays.asList( Units.CELSIUS, Units.FAHRENHEIT )) );
	
	private List<Units> unitsList;

	//CONSTRUCTORS
	private MeasurementType(List<Units> ul) {
		System.out.println("MeasurementType:CONSTRUCTOR: ul=" + ul);
		this.unitsList = ul;
	}
}

Open in new window

public class Measurement {

	private final double value;
	private final Units units;
	
	public Measurement(double value, Units units) {
		this.value = value;
		this.units = units;
	}
}

Open in new window

public class Test {

    public static void main(String[] args) {
	    Measurement measurement = new Measurement(13.1, Units.CELSIUS);
    }
}

Open in new window

Output:
MeasurementType:CONSTRUCTOR: ul=[null, null]

(was hoping for ul=[CELSIUS, FAHRENHEIT])
0
Comment
Question by:deleyd
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 40471197
I think you're trying to store a list of units in two places at once.

First in the Units enum (since that's what an enum is - a list of things) and again in MeasurementType.

To me that's a poor design and at the root of why you're getting circular definitions.  I think you should just have one list - the enum in Units.

Something like this:

public enum Units {

	FEET(MeasurementType.LENGTH),
	CELSIUS(MeasurementType.TEMPERATURE),
	FAHRENHEIT(MeasurementType.TEMPERATURE);

	// You can move this to its own class/file if you prefer but it would only
        // have this one line - don't try to move the other methods as well.
	public enum MeasurementType { TEMPERATURE, LENGTH } ;

	private final MeasurementType measurementType;

	//CONSTRUCTORS
	private Units(MeasurementType measurementType) {
		this.measurementType = measurementType;
	}

	public List<Units> getTemperatureUnits() {
		return getUnitsByMeasurementType(MeasurementType.TEMPERATURE) ;
	}

	public List<Units> getLengthUnits() {
		return getUnitsByMeasurementType(MeasurementType.LENGTH) ;
	}

	private List<Units> getUnitsByMeasurementType(MeasurementType type) {
		List<Units> units = new ArrayList<>() ;

		for (Units unit : values()) {
			if (unit.measurementType == type)
				units.add(unit) ;
		}

		return units ;
	}
}

Open in new window


Any good?

Doug
0
 

Author Closing Comment

by:deleyd
ID: 40471273
Yup that's pretty much what I figured after taking a break and eating some dinner.

I was trying to make it easy by having the info right there, and it ended up making it hard.
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40471337
Dinner solves many a hard problem :)

Doug
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now