package io.joynr.messaging.routing;

/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2017 BMW Car IT GmbH
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import static java.util.concurrent.TimeUnit.NANOSECONDS;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class TimedDelayed implements Delayed {
    static final AtomicLong sequencer = new AtomicLong();
    protected long delayUntilDate;
    protected long sequenceNumber;

    public TimedDelayed() {
        super();
    }

    public TimedDelayed(long delayForMs) {
        this.delayUntilDate = System.currentTimeMillis() + delayForMs;
        this.sequenceNumber = sequencer.getAndIncrement();
    }

    @Override
    public int compareTo(Delayed other) {
        if (other == this) {
            // equal only if the same object
            return 0;
        }

        if (other instanceof DelayableImmutableMessage) {
            DelayableImmutableMessage otherDelayedMessage = (DelayableImmutableMessage) other;
            long diffDelayedUntil = delayUntilDate - otherDelayedMessage.delayUntilDate;
            if (diffDelayedUntil < 0) {
                return -1;
            } else if (diffDelayedUntil > 0) {
                return 1;
            } else if (sequenceNumber < otherDelayedMessage.sequenceNumber) {
                return -1;
            } else {
                return 1;
            }
        }

        long diff = getDelay(NANOSECONDS) - other.getDelay(NANOSECONDS);
        return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long diff = delayUntilDate - System.currentTimeMillis();
        return unit.convert(diff, TimeUnit.MILLISECONDS);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (delayUntilDate ^ (delayUntilDate >>> 32));
        result = prime * result + (int) (sequenceNumber ^ (sequenceNumber >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        TimedDelayed other = (TimedDelayed) obj;
        if (delayUntilDate != other.delayUntilDate)
            return false;
        if (sequenceNumber != other.sequenceNumber)
            return false;
        return true;
    }
}
@@ -1431,252 +1431,128 @@
     
-if (other instanceof DelayableImmutableMessage) %7B%0A            DelayableImmutableMessage otherDelayedMessage = (DelayableImmutableMessage) other;%0A            long diffDelayedUntil = delayUntilDate - otherDelayedMessage.delayUntilDate;%0A  
+long diff = getDelay(NANOSECONDS) - other.getDelay(NANOSECONDS);%0A        if (diff %3C 0) %7B%0A            return -1;%0A
         
   if
@@ -1543,25 +1543,30 @@
 -1;%0A        
- 
+%7D else
  if (diffDel
@@ -1566,32 +1566,16 @@
 diff
-DelayedUntil %3C
+ %3E
  0) %7B%0A
-    
     
@@ -1581,33 +1581,32 @@
          return 
--
 1;%0A            %7D
@@ -1592,36 +1592,32 @@
 turn 1;%0A        
-   
- 
 %7D else if (diffD
@@ -1615,56 +1615,106 @@
 if (
-diffDelayedUntil %3E 0) %7B%0A                return 1
+other instanceof TimedDelayed) %7B%0A            TimedDelayed otherTimedDelayed = (TimedDelayed) other
 ;%0A  
@@ -1723,25 +1723,22 @@
         
-%7D else if
+return
  (sequen
@@ -1757,22 +1757,20 @@
 ther
+Timed
 Delayed
-Message
 .seq
@@ -1786,41 +1786,18 @@
 er) 
-%7B%0A                return -1;%0A    
+? -1 : 1;%0A
     
@@ -1817,36 +1817,32 @@
             
-    
 return 
-1
+0
 ;%0A          
@@ -1843,150 +1843,9 @@
     
- 
-   %7D%0A        %7D%0A%0A        long diff = getDelay(NANOSECONDS) - other.getDelay(NANOSECONDS);%0A        return (diff %3C 0) ? -1 : (diff %3E 0) ? 1 : 0;
+%7D
 %0A   

remove dependency to DelayableImmutableMessage in TimedDelayed