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
AndyC1000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
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
AndyC1000Author Commented:
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.
ste5anSenior DeveloperCommented:
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.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

AndyC1000Author Commented:
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.
ste5anSenior DeveloperCommented:
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.
AndyC1000Author Commented:
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.
dpearsonCommented:
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.

Yeah - that's why option (b) is a more work and touching more code.  You need to go find all the places using the constant and modify them to use a method.

If you want it can be a method on a singleton class:

public class Constants {
   private int m_pointsPerDay = 24 ;  // Default is 24 but now it can be changed
   public void setPointsPerDay(int n) { m_pointsPerDay = n ; }
   public int getPointsPerDay() { return m_pointsPerDay ; }
}

// This is the singleton
public static final Constants kGlobalConstants = new Constants() ;

and then in code it becomes:
kGlobalConstants.getPointsPerDay() ;
where today you have:
IConstants.DATA_POINTS_PER_DAY ;

Now on startup when the user selects the value you call:
kGlobalConstants.setPointsPerDay(48) ;

You need to do that *before* any of the other code calls to get this value, otherwise some will see 24 and some 48.

Hope that helps,

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.