Solved

Java circular enum constructors (catch 22)

Posted on 2014-11-28
3
176 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running Jira on Raspberry PI 2? 3 247
DO we need Java installed on a Windows PC and WHY ? 13 94
GUI builder for Eclipse? 8 27
expectj telnet failing 5 17
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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

930 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

12 Experts available now in Live!

Get 1:1 Help Now