Solved

Java circular enum constructors (catch 22)

Posted on 2014-11-28
3
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

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 27

Expert Comment

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

Doug
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

737 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