How to relate classes in java? - java

I wrote time conversion program(i.e.,seconds to minute and minute seconds etc), but later I found that these classes perform similar operation.Is there any way to relate these classes, if so please give some solution and suggestion. Here is my code....
Second.java
import java.util.concurrent.*;
public class Second {
private long secondsValue;
public Second() {
secondsValue = 0L;
}
public Second(String from, String to, long unitValue) {
unitSelection(from, to, unitValue);
}
private void convertSecondToMinute(long unitValue) {
unitValue = TimeUnit.MINUTES.convert(unitValue, TimeUnit.SECONDS);
secondsValue = unitValue;
}
private void convertSecondToHour(long unitValue) {
unitValue = TimeUnit.HOURS.convert(unitValue, TimeUnit.SECONDS);
secondsValue = unitValue;
}
private void convertSecondToDay(long unitValue) {
unitValue = TimeUnit.DAYS.convert(unitValue, TimeUnit.SECONDS);
secondsValue = unitValue;
}
private void convertSecondToWeek(long unitValue) {
unitValue = unitValue/60/60/24/7;
secondsValue = unitValue;
}
public long getSeconds() {
return secondsValue;
}
private void unitSelection(String from, String to,
long unitValue) {
if( from.equalsIgnoreCase("second")) {
if(to.equalsIgnoreCase("minute")) {
convertSecondToMinute(unitValue);
}
else if(to.equalsIgnoreCase("hour")) {
convertSecondToHour(unitValue);
}
else if(to.equalsIgnoreCase("day")) {
convertSecondToDay(unitValue);
}
else if(to.equalsIgnoreCase("week") ) {
convertSecondToWeek(unitValue);
}
else {
System.out.println("Invalid argument...!");
}
}
}
}
Minute.java
import java.util.concurrent.TimeUnit;
public class Minute {
private long unitMinute;
public Minute() {
unitMinute = 0L;
}
public Minute(String from, String to,
long unitValue) {
unitSelection(from, to, unitValue);
}
private void convertMinuteToSecond(long unitValue) {
unitValue = TimeUnit.SECONDS.convert(unitValue, TimeUnit.MINUTES);
unitMinute = unitValue;
}
private void convertMinuteToHour(long unitValue) {
unitValue = TimeUnit.HOURS.convert(unitValue, TimeUnit.MINUTES);
unitMinute = unitValue;
}
private void convertMinuteToDay(long unitValue) {
unitValue = TimeUnit.DAYS.convert(unitValue, TimeUnit.MINUTES);
unitMinute = unitValue;
}
private void convertMinuteToWeek(long unitValue) {
long value = unitValue /60/24/7;
unitMinute = value;
}
public long getUnitMinute() {
return unitMinute;
}
private void unitSelection(String from, String to,
long unitValue) {
if( from.equalsIgnoreCase("minute")) {
if(to.equalsIgnoreCase("second")) {
convertMinuteToSecond(unitValue);
}
else if(to.equalsIgnoreCase("hour")) {
convertMinuteToHour(unitValue);
}
else if(to.equalsIgnoreCase("day")) {
convertMinuteToDay(unitValue);
}
else if(to.equalsIgnoreCase("week") ) {
convertMinuteToWeek(unitValue);
}
else {
System.out.println("Invalid argument...!");
}
}
}
}

The best way to improve your code is to delete all of your classes and use TimeUnit instead.
TimeUnit has all this functionality (and more) and comes with the JDK.
Don't reinvent the wheel.

When you say "related" is very wide topic. However, the right way to start is using java Packaging mechanism to relate classes at very highest level. You can manage your code (if possible) using Inheritance and Polymorphism . As #Bohemian suggested , use existing java libraries until or unless your are doing sth new and doing it for learning purpose.

I see 2 options:
1. Re-define both as a single class: which suggest the use of a universal value standard (number of milliseconds for example), and to offer what it means in different measures like minutes, hours, ..etc
class Duration
{
private long secondsValue;
public Duration() {
secondsValue = 0L;
}
public Duration(String format, long value) {
if (format.equals("SECONDS")) secondsValue = value;
else if (format.equals("MINUTES")) secondsValue = value*60;
else if (format.equals("HOURS")) secondsValue = value*60*60;
else if (format.equals("DAYS")) secondsValue = value*60*60*24;
else if (format.equals("WEEKS")) secondsValue = value*60*60*24*7;
}
public long asMinutes() {
return value/60;
}
public long asHours() {
return value/(60*60);
}
public long asDays() {
return value/(60*60*24);
}
public long asWeeks() {
return unitValue/(60*60*24*7);
}
public long asSeconds() {
return secondsValue;
}
}
2. use a converter utility class: like how you use TimeUnit.MINUTES.convert(unitValue, TimeUnit.SECONDS); to convert from seconds to minutes

Related

Enums and State... Using a listener to time it - Java

public class X {
private long timeInactive = 0;
private final long timeStarted = System.currentTimeMillis();
private enum State {
ACTIVE,INACTIVE
};
private State getState() {
if(Math.random <= 0.1) {
return State.ACTIVE;
}
return State.INACTIVE;
}
public static void main(String[] args) {
//code
}
}
Let's pretend I had something like this. How would I input a listener that would time how long the state was inactive before once again becoming active, and do this every single time it went back to being inactive?
Edit: I need to know when getState() has changed value after being changed by random or external factors. For instance, in psvm, I might have something like:
public static void main(String[] args) {
while(true) {
if(value of getState() has changed from inactive to active) {
System.out.println(How long it was inactive prior);
}
}
}
Thanks!
keep the time inside X without Listner
public class X {
private long timeInactive = 0; // total time of being inactive
private long timeLastChanged = System.currentTimeMillis();
private State currentState = State.INACTIVE;
enum State {
ACTIVE, INACTIVE
}
public State getState() {
changeState(); // It shouldn't be called here. I just need to simulate the state changing inside randomly
return currentState;
}
private void changeState() {
State oldState = currentState;
if (Math.random() <= 0.1) {
currentState = State.ACTIVE;
} else {
currentState = State.INACTIVE;
}
long now = System.currentTimeMillis();
if (oldState == State.INACTIVE && currentState == State.ACTIVE) {
long elapse = now - timeLastChanged;
timeInactive += elapse;
}
if (oldState != currentState) {
timeLastChanged = now;
}
}
public long getTotalTimeInActive() {
return timeInactive;
}
public static void main(String[] args) throws InterruptedException {
X x = new X();
while (true) {
x.getState();
System.out.println(x.getTotalTimeInActive());
Thread.sleep(1000);
}
}
}
With a listener:
//Listner.java
interface Listener {
void onStateChange(X2.State currentState);
}
//X2.java
import java.util.ArrayList;
import java.util.List;
public class X2 {
private State currentState = State.INACTIVE;
private List<Listener> listeners = new ArrayList<>();
enum State {
ACTIVE, INACTIVE
}
public State getState() {
changeState(); // It shouldn't be called here. I just need to simulate the state changing inside randomly
return currentState;
}
private void changeState() {
if (Math.random() <= 0.1) {
currentState = State.ACTIVE;
} else {
currentState = State.INACTIVE;
}
for(Listener listener:listeners){
listener.onStateChange(currentState);
}
}
public void registerListener(Listener listener){
listeners.add(listener);
}
public static void main(String[] args) throws InterruptedException {
X2 x = new X2();
TimeInactiveListener timeInactiveListener = new TimeInactiveListener();
x.registerListener(timeInactiveListner);
while (true) {
x.getState();
System.out.println(timeInactiveListner.getTimeInactive());
Thread.sleep(1000);
}
}
}
class TimeInactiveListener implements Listener {
private long timeLastChanged = System.currentTimeMillis();
private X2.State oldState = X2.State.INACTIVE;
private long timeInactive = 0; // total time of being inactive
#Override
public void onStateChange(X2.State currentState) {
long now = System.currentTimeMillis();
if (oldState == X2.State.INACTIVE && currentState == X2.State.ACTIVE) {
long elapse = now - timeLastChanged;
timeInactive += elapse;
}
if (oldState != currentState) {
timeLastChanged = now;
}
oldState = currentState;
}
public long getTimeInactive() {
return timeInactive;
}
}
If you wanted to know when it changed states you would have to know what the last state was so you have something to compare it to. And then just check if the new state is different then the old state, and if so then record the time inactive.
private State state;
public void setState(State newstate){
if (!newstate.equals(state)){
if (state.equals(State.INACTIVE)){
timeInactive = 0;
timeStarted = System.currentTimeMillis();
}
else if (state.equals(State.ACTIVE)){
timeInactive = System.currentTimeMillis()-timeStarted;
}
}
state = newstate;
}
Also to do this you would need to change timeStarted to not be final.
At the moment you have nothing in your code that records the current state. You are defining a private enum but that is nothing more than declaring that a state can have two values: it doesn't actually record the state. To do that you'd need a field with state defined and the last time in changed state. Also note that by making it private it can only be used internally in the class. That might work in this situation (because main is part of the same class) but will generally not be what you want.
Ideally you should encapsulate that logic in a State class rather than have it in the class using the state.
public class State {
public enum Value { ACTIVE, INACTIVE };
private Value value = Value.INACTIVE;
private long inactiveTime = System.currentTimeMillis();
public Value getValue() {
Value newValue = Math.random() < 0.1 ? Value.ACTIVE : Value.INACTIVE;
if (newValue != value) {
if (newValue == Value.ACTIVE)
// notify listener with inactive period current - inactiveTime
else
inactiveTime = System.currentTimeMillis();
value = newValue;
}
return value;
}
}

state maintaining variable which goes back to the previous state after a time

Is it possible for a variable in java to revert back to its previous state after a time limit.For example:If I have a boolean variable say x which was initialized to false and later the value was changed to true.Is it possible for this variable to revert back to its original value (ie false) after a time limit.Can this be achieved without using a timer or are there any design patterns to achieve this functionality.
Find a small example of a class which keeps the state and based on the timeout it negates the state.
See this as a PoC. You might need to do some improvements for concurrent access or visibility if you want to use it in a multithreaded application.
public static void main(String[] args) throws Exception {
VolatileState volatileState = new VolatileState();
System.out.println("initial: " + volatileState.getState());
volatileState.setState(true, 5);
System.out.println("valid : " + volatileState.getState());
TimeUnit.SECONDS.sleep(10);
System.out.println("reset : " + volatileState.getState());
}
The class which keeps the state for a given time.
class VolatileState {
private long timeSet = Long.MAX_VALUE;
private long timeToLive = 0;
private boolean state;
/**
* Keep the given state for <code>timeToLife</code> seconds.
*/
void setState(boolean state, long timeToLive) {
this.timeSet = System.currentTimeMillis();
this.timeToLive = TimeUnit.MILLISECONDS.toSeconds(timeToLive);
}
boolean getState() {
if (System.currentTimeMillis() > timeSet + timeToLive ) {
state = !state;
System.out.println("state reset to " + state);
}
return state;
}
}
You can use something like this:
class RevertingBoolean {
private long timeToRevert = Long.MAX_VALUE;
private boolean value;
private boolean defaultValue;
void setDefaultValue(boolean value) {
this.defaultValue = value;
}
void setRevertAfter(long revertAfter) {
this.timeToRevert = System.currentTimeMillis() + revertAfter;
}
void setValue(boolean value) {
this.value = value;
}
boolean getValue() {
if (System.currentTimeMillis() > timeToRevert) {
this.value = this.defaultValue;
timeToRevert = Long.MAX_VALUE;
}
return this.value;
}
}
Usage:
RevertingBoolean myBool = new RevertingBoolean();
myBool.setDefaultValue(false);
myBool.setValue(false);
myBool.setRevertAfter(10000); // Revert value in 10 seconds
myBool.getValue(); // false
myBool.setValue(true);
myBool.getValue(); // true
// ... 10 seconds later ...
myBool.getValue(); // false
You can create a class, e.g., BooleanReverts, which has members for the new value, old value to which it reverts, and time to revert, and with setter methods that set up reversion. You probably want a separate thread to manage all the timers and execute callbacks to effect the reversion.
here is a simple generic class and a Test case with a boolean type (but you can use with any type) of a class that shoud match your requirements
package test;
public class TimeValueVar<T> {
private T originalValue;
private T currentValue;
private long duration;
private long creationTime;
public TimeValueVar(T value, long duration) {
originalValue=value;
currentValue=value;
this.duration=duration;
creationTime=System.currentTimeMillis();
}
public void set(T value) {
currentValue=value;
}
public T get() {
if ((System.currentTimeMillis()-creationTime)>duration) {
return originalValue;
}
return currentValue;
}
}
here is the test case
package test;
import java.util.concurrent.TimeUnit;
public class TesttimeBasedValue {
public static void main(String[] args) throws Exception {
long duration =2000;
TimeValueVar<Boolean> boolVal=new TimeValueVar<>(true,duration);
System.out.println("original: " + boolVal.get());
boolVal.set(false);
System.out.println("before duration : " + boolVal.get());
TimeUnit.MILLISECONDS.sleep(duration+1);
System.out.println("after duration : " + boolVal.get());
}
}

Supplying call latency as a IntStream

I am trying to make use of Java 8 and streams and one of the things I am trying to replace is a system we have where we
Use an aspect to measure call latency (per config period of time) to out webservices and then
Feed those results into a Complex Event Processor (esper) so that
We can send out alert notifications
So, one step at a time. For the first step, I need to produce a stream (I think) that allows me to feed those latency numbers into existing listeners. Understanding that, getting the next number in series might have to wait until there is a call.
How can I do that? Here is the latency aspect with comments.
public class ProfilingAspect {
private ProfilingAction action;
public ProfilingAspect(ProfilingAction action) {
this.action = action;
}
public Object doAroundAdvice(ProceedingJoinPoint jp) throws Throwable{
long startTime = System.currentTimeMillis();
Object retVal = null;
Throwable error = null;
try{
retVal = jp.proceed();
}catch (Throwable t){
error = t;
}
Class withinType = jp.getSourceLocation().getWithinType();
String methodName = jp.getSignature().getName();
long endTime = System.currentTimeMillis();
long runningTime = endTime - startTime;
// Let the IntStream know we have a new latency. Or really, we have an object
// stream with all this extra data
action.perform(withinType, methodName, jp.getArgs(), runningTime, error);
if( error != null ){
throw error;
}
return retVal;
}
}
Ok, I have a working example. It doesn't handle the situation where I have to buffer up results though is the stream isn't being read fast enough. I am open to some improvement
public class LatencySupplier implements Supplier<SomeFancyObject> {
private Random r = new Random();
#Override
public SomeFancyObject get() {
try {
Thread.sleep(100 + r.nextInt(1000));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return new SomeFancyObject(10 + r.nextInt(1000));
}
}
public class SomeFancyObject {
private static String[] someGroups = {"Group1","Group2","Group3"};
private final String group;
private int value;
public SomeFancyObject(int value) {
this.value = value;
this.group = WSRandom.selectOne(someGroups);
}
public String getGroup() {
return group;
}
public int getValue() {
return value;
}
#Override
public String toString() {
return value + "";
}
}
My next step is to create a stream by time so I can do avg/5 min, etc.
public class Sample {
public static void main(String[] args) throws InterruptedException {
Stream<SomeFancyObject> latencyStream = Stream.generate(new LatencySupplier());
Map<Object,List<SomeFancyObject>> collect = latencyStream.limit(10).collect(Collectors.groupingBy(sfo -> sfo.getGroup()));
System.out.println(collect);
Object o = new Object();
synchronized (o){
o.wait();
}
}
}

Where's the error in this piece of threaded code?

I created a class which is used as a cache provider. It uses a Map, timestamped map entries and it spawns a Thread which performs cleanup every so often. This class is used in a web application. This web application had a problem where POST would take 30 seconds. I traced the problem to this cache class, eliminating it resolves the problem.
I have tried my best to find the error in this class but I can't. Please help me out here.
Assume User class is some kind of POJO describing the user.
public class UserStore implements Thread.UncaughtExceptionHandler {
private static volatile UserStore instance;
private static Thread cleanUpThread;
private static Map<String, TimeStampedToken<User>> tokenMap = new HashMap<String, TimeStampedToken<User>>();
public static UserStore getInstance() {
if (instance == null) {
synchronized(UserStore.class) {
if (instance == null) {
instance = new UserStore();
cleanUpThread = new Thread(new CleanUpWorker());
cleanUpThread.setUncaughtExceptionHandler(instance);
cleanUpThread.start();
}
}
}
return instance;
}
public void uncaughtException(Thread thread, Throwable throwable) {
if (throwable instanceof ThreadDeath) {
cleanUpThread = new Thread(new CleanUpWorker());
cleanUpThread.setUncaughtExceptionHandler(this);
cleanUpThread.start();
throw (ThreadDeath)throwable;
}
}
private static class CleanUpWorker implements Runnable {
private static final long CLEANUP_CYCLE_MS = 300000;
private static final long OBJECT_LIVE_TIME = 299900;
public void run() {
long sleepRemaining;
long sleepStart = System.currentTimeMillis();
sleepRemaining = CLEANUP_CYCLE_MS;
while (true) {
try {
sleepStart = System.currentTimeMillis();
Thread.sleep(sleepRemaining);
cleanUp();
sleepRemaining = CLEANUP_CYCLE_MS;
} catch (InterruptedException e) {
sleepRemaining = System.currentTimeMillis() - sleepStart;
}
}
}
private void cleanUp() {
Long currentTime = System.currentTimeMillis();
synchronized(tokenMap) {
for (String user : tokenMap.keySet()) {
TimeStampedToken<User> tok = tokenMap.get(user);
if (tok.accessed + OBJECT_LIVE_TIME < currentTime) {
tokenMap.remove(user);
}
}
}
}
}
public void addToken(User tok) {
synchronized(tokenMap) {
tokenMap.put(tok.getUserId(), new TimeStampedToken<User>(tok));
}
}
public User getToken(String userId) {
synchronized(tokenMap) {
TimeStampedToken<User> user = tokenMap.get(userId);
if (user != null) {
user.accessed = System.currentTimeMillis();
return user.payload;
} else {
return null;
}
}
}
private static class TimeStampedToken<E> {
public TimeStampedToken(E payload) {
this.payload = payload;
}
public long accessed = System.currentTimeMillis();
public E payload;
}
}
Here is how I would approach it. With multi-threaded code, simplicity is often the best approach as its more likely to work.
(the third parameter trueof the LinkedHashMap means that iterators over this Map follow the order of access rather than order of insertion)
public enum UserStore {
;
interface User {
String getUserId();
}
// a LRU cache with a timestamp.
private static final Map<String, TimeStampedToken<User>> tokenMap = new LinkedHashMap<String, TimeStampedToken<User>>(16, 0.7f, true);
private static final long OBJECT_LIVE_TIME = 299900;
public static synchronized void addToken(User tok) {
final long now = System.currentTimeMillis();
// clean up as we go
for (Iterator<Map.Entry<String, TimeStampedToken<User>>> iter = tokenMap.entrySet().iterator(); iter.hasNext(); ) {
final Map.Entry<String, TimeStampedToken<User>> next = iter.next();
if (next.getValue().accessed + OBJECT_LIVE_TIME >= now)
// the map is ordered by access time so there are no more to clean up.
break;
iter.remove();
}
// add a new entry
tokenMap.put(tok.getUserId(), new TimeStampedToken<User>(tok, now));
}
public static synchronized User getToken(String userId) {
final long now = System.currentTimeMillis();
TimeStampedToken<User> user = tokenMap.get(userId);
if (user == null)
return null;
user.accessed = now;
return user.payload;
}
static class TimeStampedToken<E> {
long accessed;
final E payload;
TimeStampedToken(E payload, long now) {
this.payload = payload;
accessed = now;
}
}
}
This line looks weird to me...
sleepRemaining = System.currentTimeMillis() - sleepStart;
...surely it should be...
sleepRemaining = CLEANUP_CYCLE_MS - (System.currentTimeMillis() - sleepStart);

What would you like to correct and/or improve in this java implementation of Chain Of Responsibility?

package design.pattern.behavioral;
import design.pattern.behavioral.ChainOfResponsibility.*;
public class ChainOfResponsibility {
public static class Chain {
private Request[] requests = null;
private Handler[] handlers = null;
public Chain(Handler[] handlers, Request[] requests){
this.handlers = handlers;
this.requests = requests;
}
public void start() {
for(Request r : requests)
for (Handler h : handlers)
if(h.handle(r)) break;
}
}
public static class Request {
private int value;
public Request setValue(int value){
this.value = value;
return this;
}
public int getValue() {
return value;
}
}
public static class Handler<T> {
private Command<T> command = null;
public Handler(Command<T> command) {
this.command = command;
}
public boolean handle(T request) {
return command.execute(request);
}
}
public static abstract class Command<T>{
public abstract Boolean execute(T request);
}
}
class TestChainOfResponsibility {
public static void main(String[] args) {
new TestChainOfResponsibility().test();
}
private void test() {
new Chain(new Handler[]{ // chain of responsibility
new Handler<Request>(
new Command<Request>(){ // command
public Boolean execute(Request condition) {
boolean result = condition.getValue() >= 600;
if (result) System.out.println("You are rich: " + condition.getValue() + " (id: " + condition.hashCode() + ")");
return result;
}
}
),
new Handler<Request>(
new Command<Request>(){
public Boolean execute(Request condition) {
boolean result = condition.getValue() >= 100;
if(result) System.out.println("You are poor: " + condition.getValue() + " (id: " + condition.hashCode() + ")");
return result;
}
}
),
},
new Request[]{
new Request().setValue(600), // chaining method
new Request().setValue(100),
}
).start();
}
}
I don't think there is a meaningful answer to such a general question. Design patterns don't exist in isolation and don't have a "perfect form": they live in a context.
A pattern is a solution to a problem in a context.
So without knowing the context of your solution, there is not much we can say about it. What is the concrete problem you are trying to resolve with it? What forces are in play? What are your constraints? Do you have any problems / issues with the current solution? If you give more details about these, maybe we can give a better answer.
Lambda isn't very descriptive (to most developers). Is it something you are pulling in from functional language theory?
I'd probably just get rid of the 'controlling' class, and wire the individual handlers up to each other directly - use more of an IoC approach, basically.
Example (in C#, forgive me) per request...
public interface IIntMessage
{
void HandleMesasge(int i);
}
public class EvenPrinter : IIntMessage
{
private IIntMessage m_next;
public EvenPrinter(IIntMessage next)
{
m_next = next;
}
public void HandleMesasge(int i)
{
if(i % 2 == 0)
{
System.Console.WriteLine("Even!");
}
else
{
m_next.HandleMesasge(i);
}
}
}
public class OddPrinter : IIntMessage
{
private IIntMessage m_next;
public OddPrinter(IIntMessage next)
{
m_next = next;
}
public void HandleMesasge(int i)
{
if(i%2 == 1)
{
System.Console.WriteLine("Odd!");
}
else
{
m_next.HandleMesasge(i);
}
}
}
Note that we get rid of the "controlling" class altogether, and simply allow the request handlers to directly chain to each other, without having to go through an intermediary.
Also, I could probably extract out a 'base' chain-of-command request handler, removing some of the duplicate code.

Categories