001 /**
002 * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003 *
004 * All rights reserved. Licensed under the OSI BSD License.
005 *
006 * http://www.opensource.org/licenses/bsd-license.php
007 */
008 package com.barchart.udt;
009
010 public class LingerUDT extends Number implements Comparable<LingerUDT> {
011
012 public static final LingerUDT LINGER_ZERO = new LingerUDT(0);
013
014 // measured in seconds
015 final int timeout;
016
017 /**
018 * Default constructor. NOTE: linger value is "u_short" on windows and "int"
019 * on linux:<br>
020 * Windows: <a
021 * href="http://msdn.microsoft.com/en-us/library/ms739165(VS.85).aspx">
022 * linger Structure on Windows</a><br>
023 * Linux: <a href=
024 * "http://www.gnu.org/s/libc/manual/html_node/Socket_002dLevel-Options.html"
025 * >GCC Socket-Level Options</a><br>
026 * Therefore select smallest range: 0 <= linger <= 65535 <br>
027 *
028 * @param lingerSeconds
029 * the seconds to linger; "0" means "do not linger"
030 *
031 * @throws IllegalArgumentException
032 * when lingerSeconds is out of range
033 */
034 public LingerUDT(int lingerSeconds) throws IllegalArgumentException {
035 if (65535 < lingerSeconds) {
036 throw new IllegalArgumentException(
037 "lingerSeconds is out of range: 0 <= linger <= 65535");
038 }
039 this.timeout = lingerSeconds > 0 ? lingerSeconds : 0;
040 }
041
042 private static final long serialVersionUID = 3414455799823407217L;
043
044 @Override
045 public double doubleValue() {
046 return timeout;
047 }
048
049 /*
050 * (non-Javadoc)
051 *
052 * @see java.lang.Number#floatValue()
053 */
054 @Override
055 public float floatValue() {
056 return timeout;
057 }
058
059 @Override
060 public int intValue() {
061 return timeout;
062 }
063
064 @Override
065 public long longValue() {
066 return timeout;
067 }
068
069 boolean isLingerOn() {
070 return timeout > 0;
071 }
072
073 int timeout() {
074 return timeout;
075 }
076
077 @Override
078 public boolean equals(Object otherLinger) {
079 if (otherLinger instanceof LingerUDT) {
080 LingerUDT other = (LingerUDT) otherLinger;
081 return other.timeout == this.timeout;
082 }
083 return false;
084 }
085
086 @Override
087 public int hashCode() {
088 return timeout;
089 }
090
091 @Override
092 public int compareTo(LingerUDT other) {
093 return other.timeout - this.timeout;
094 }
095
096 @Override
097 public String toString() {
098 return String.valueOf(timeout);
099 }
100
101 }