Link to home
Start Free TrialLog in
Avatar of AndyC1000
AndyC1000

asked on

Java programming design help

Dear all,

I've inherited a large piece of code which processes data at an hourly time step.  
The requirement is to update the code to process data at a half hourly time step.  I've tracked down the variable that I need to update, it’s a variable in an interface that has a fixed value of 24.  See below example.
public interface IConstants {

EReference[] BREAKDOWN_REFS = new EReference[] {
HPackage.eINSTANCE.getBreakdown_Total(),
HPackage.eINSTANCE.getBreakdown_Average(),
HPackage.eINSTANCE.getBreakdown_Min()};
int DATA_POINT_PER_DAY = 24;
int DAYS_PER_YEAR = 365;
int DATA_POINT_PER_YEAR = DATA_POINT_PER_DAY * DAYS_PER_YEAR;
}

Open in new window

The data in the interface are accessed within the code base in a static way i.e. IConstants.DATA_POINT_PER_DAY
I have added a program argument and saved it to an integer variable named timesteps in the main class of the program.  I’m not sure how to set DATA_POINT_PER_DAY to this value. If I create a set method in the IConstants interface I need to update the implementable class that it’s linked to, it was created using an EMF model that I don’t want to make changes to.

I created a class called ModelTimesteps with the basic set and get variables for timesteps, not sure how to pass the reference of the ModelTimesteps class to the IConstants interface to set the value of DATA_POINT_PER_DAY.  IConstants.DATAPOINT per day is also accessed in too many places to count in the code.

public class ModelTimestep {
		private int timestep;

		public int getTimestep() {
			return timestep;
		}

		public void setTimestep(int timestep) {
			this.timestep = timestep;
		}

	}

Open in new window


I thought about creating an inner class the same at the ModelTimesteps class described above.  But that isn’t very good practice.   If I go ahead with the inner class how do I set it in the main class and update the value of DATA_POINT_PER_DAY?  I tried IHhmodelConstants.ModelTimestep.this.setTimestep(timestep); in the main and the error states " no enclosing instance of the type is accessible in the scope".
 
What do you think any ideas? I'm not sure how to go about this.

Thanks
Avatar of dpearson
dpearson

If the code is accessing the value directly from the interface like this:
IConstants.DATA_POINT_PER_DAY
as you say it is, then you can't modify that value.  It's a final constant when it's in an interface.  This is the way constants used to often be defined in Java before enums existed.  But the key point is they are constants - not modifiable in code at all.

So I think your only options are:

a) Modify it directly in the interface
public interface IConstants {
...
int DATA_POINT_PER_DAY = 48; // We're going faster now
}

b) Modify all of the code that refers to this constant to instead get the value from a method which you can then override:
int rate = new Constants().getDataPointsPerDay() ;

(This may be a big project).

I'm not clear from what you posted on why you can't do (a) and just edit the value in the interface?

Doug
Avatar of AndyC1000

ASKER

Thanks for your response.  I was hoping to somehow override the value.  You've raised a very good point about option a.  I forgot to mention the reason why I created a program argument to store the value of timestep is because the code will still need process data at the hourly timescale too.  The user will either enter 24 or 48.

In your example, int rate = new Constants().getDataPointsPerDay() ; I'm not sure how to ensure the instance of Constants is accessible in the all places DATA_POINTS_PER_DAY is used in the code.
Please confirm you requirements. Cause

The requirement is to update the code to process data at a half hourly time step

means to process/calculate/load it more often in the same time span.

Doubling that constant to increase the data volume.

These are different things.
It's how the code works, I've changed DATA_POINTS_PER_DAY to 48 and tested it. Haven't been able to set the value to the time steps program argument.
It is a semantical issue. This is more than just nitpicking.

DATA_POINTS_PER_DAY only denotes amount of data.

When this is the only adjustable screw, then it's imho important to review the requirements. Especially as your customer isn't aware of this problem.

btw, have you tested it already? Because I really assume that you will have in the end the double amount of data.
And this result (double amount of data) is not covered by your requirement.
Even when this will result in code executed in an half hourly step.

Thus you need to review it.
The DATA_POINTS_PER_DAY variable is used to read the input files containing data at half hourly time step and for charting.  I've tested the output after setting the value to 48.
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial