Shuyi Chen | d7955ce | 2013-05-22 14:51:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Mike Cumings |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.kenai.jbosh; |
| 18 | |
| 19 | /** |
| 20 | * Abstract base class for creating BOSH attribute classes. Concrete |
| 21 | * implementations of this class will naturally inherit the underlying |
| 22 | * type's behavior for {@code equals()}, {@code hashCode()}, |
| 23 | * {@code toString()}, and {@code compareTo()}, allowing for the easy |
| 24 | * creation of objects which extend existing trivial types. This was done |
| 25 | * to comply with the prefactoring rule declaring, "when you are being |
| 26 | * abstract, be abstract all the way". |
| 27 | * |
| 28 | * @param <T> type of the extension object |
| 29 | */ |
| 30 | abstract class AbstractAttr<T extends Comparable> |
| 31 | implements Comparable { |
| 32 | |
| 33 | /** |
| 34 | * Captured value. |
| 35 | */ |
| 36 | private final T value; |
| 37 | |
| 38 | /** |
| 39 | * Creates a new encapsulated object instance. |
| 40 | * |
| 41 | * @param aValue encapsulated getValue |
| 42 | */ |
| 43 | protected AbstractAttr(final T aValue) { |
| 44 | value = aValue; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Gets the encapsulated data value. |
| 49 | * |
| 50 | * @return data value |
| 51 | */ |
| 52 | public final T getValue() { |
| 53 | return value; |
| 54 | } |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////// |
| 57 | // Object method overrides: |
| 58 | |
| 59 | /** |
| 60 | * {@inheritDoc} |
| 61 | * |
| 62 | * @param otherObj object to compare to |
| 63 | * @return true if the objects are equal, false otherwise |
| 64 | */ |
| 65 | @Override |
| 66 | public boolean equals(final Object otherObj) { |
| 67 | if (otherObj == null) { |
| 68 | return false; |
| 69 | } else if (otherObj instanceof AbstractAttr) { |
| 70 | AbstractAttr other = |
| 71 | (AbstractAttr) otherObj; |
| 72 | return value.equals(other.value); |
| 73 | } else { |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * {@inheritDoc} |
| 80 | * |
| 81 | * @return hashCode of the encapsulated object |
| 82 | */ |
| 83 | @Override |
| 84 | public int hashCode() { |
| 85 | return value.hashCode(); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritDoc} |
| 90 | * |
| 91 | * @return string representation of the encapsulated object |
| 92 | */ |
| 93 | @Override |
| 94 | public String toString() { |
| 95 | return value.toString(); |
| 96 | } |
| 97 | |
| 98 | /////////////////////////////////////////////////////////////////////////// |
| 99 | // Comparable interface: |
| 100 | |
| 101 | /** |
| 102 | * {@inheritDoc} |
| 103 | * |
| 104 | * @param otherObj object to compare to |
| 105 | * @return -1, 0, or 1 |
| 106 | */ |
| 107 | @SuppressWarnings("unchecked") |
| 108 | public int compareTo(final Object otherObj) { |
| 109 | if (otherObj == null) { |
| 110 | return 1; |
| 111 | } else { |
| 112 | return value.compareTo(otherObj); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } |