001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.support; 018 019 import java.util.Map; 020 021 import org.apache.camel.TimeoutMap; 022 023 /** 024 * Represents an entry in a {@link TimeoutMap} 025 * 026 * @version 027 */ 028 public class TimeoutMapEntry<K, V> implements Comparable<Object>, Map.Entry<K, V> { 029 private K key; 030 private V value; 031 private long timeout; 032 private long expireTime; 033 034 public TimeoutMapEntry(K id, V handler, long timeout) { 035 this.key = id; 036 this.value = handler; 037 this.timeout = timeout; 038 } 039 040 public K getKey() { 041 return key; 042 } 043 044 public long getExpireTime() { 045 return expireTime; 046 } 047 048 public void setExpireTime(long expireTime) { 049 this.expireTime = expireTime; 050 } 051 052 public V getValue() { 053 return value; 054 } 055 056 public V setValue(V value) { 057 V oldValue = this.value; 058 this.value = value; 059 return oldValue; 060 } 061 062 public long getTimeout() { 063 return timeout; 064 } 065 066 public void setTimeout(long timeout) { 067 this.timeout = timeout; 068 } 069 070 @SuppressWarnings("unchecked") 071 public int compareTo(Object that) { 072 if (this == that) { 073 return 0; 074 } 075 if (that instanceof TimeoutMapEntry) { 076 return compareTo((TimeoutMapEntry<K, V>) that); 077 } 078 return 1; 079 } 080 081 public int compareTo(TimeoutMapEntry<K, V> that) { 082 long diff = this.expireTime - that.expireTime; 083 if (diff > 0) { 084 return 1; 085 } else if (diff < 0) { 086 return -1; 087 } 088 return this.key.hashCode() - that.key.hashCode(); 089 } 090 091 public String toString() { 092 return key + " (times out after " + timeout + " millis)"; 093 } 094 }