Solved

Java circular enum constructors (catch 22)

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

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 28

Expert Comment

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

Doug
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

717 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