NameValuePair.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.model;

  19. import org.apache.commons.lang.builder.ToStringBuilder;

  20. import java.io.Serializable;
  21. import java.util.Comparator;

  22. /**
  23.  * Class provides basic storage for name values pairs. The name is usually a key
  24.  * of some type, like a status number, and the value is a localized name for
  25.  * that key.
  26.  */
  27. public class NameValuePair extends AbstractEntity {

  28.     /**
  29.      *
  30.      */
  31.     private static final long serialVersionUID = 1L;

  32.     private String name = "";

  33.     private String value = "";

  34.     private static final class NameComparator implements Comparator<NameValuePair>, Serializable {
  35.         /**
  36.          *
  37.          */
  38.         private static final long serialVersionUID = 1L;

  39.         public int compare(NameValuePair o1, NameValuePair o2) {

  40.             return o1.name.compareTo(o2.name);
  41.         }

  42.         ;
  43.     }

  44.     private static final class ValueComparator implements Comparator<NameValuePair>, Serializable {
  45.         /**
  46.          *
  47.          */
  48.         private static final long serialVersionUID = 1L;

  49.         public int compare(NameValuePair o1, NameValuePair o2) {
  50.             return o1.value.compareTo(o2.value);
  51.         }

  52.         ;
  53.     }

  54.     public static final Comparator<NameValuePair> KEY_COMPARATOR = new NameComparator();
  55.     public static final Comparator<NameValuePair> VALUE_COMPARATOR = new ValueComparator();

  56.     public NameValuePair(String name, String value) {
  57.         setName(name);
  58.         setValue(value);
  59.     }

  60.     /**
  61.      * Returns the name of the name/value pair.
  62.      */
  63.     public String getName() {
  64.         return name;
  65.     }

  66.     /**
  67.      * Sets the name of the name/value pair.
  68.      */
  69.     public void setName(String name) {
  70.         if (name == null) {
  71.             name = "";
  72.         }
  73.         this.name = name;
  74.     }

  75.     /**
  76.      * Returns the value of the name/value pair.
  77.      */
  78.     public String getValue() {
  79.         return value;
  80.     }

  81.     /**
  82.      * Sets the value of the name/value pair.
  83.      */
  84.     public void setValue(String value) {
  85.         this.value = value;
  86.     }

  87.     public int compareKeyTo(NameValuePair other) {
  88.         return KEY_COMPARATOR.compare(this, other);
  89.     }

  90.     public int compareValueTo(NameValuePair other) {
  91.         return VALUE_COMPARATOR.compare(this, other);
  92.     }


  93. //
  94. //  @Override
  95. //  public boolean equals(Object obj) {
  96. //      if (this == obj) {
  97. //          return true;
  98. //      }
  99. //
  100. //      if (obj instanceof NameValuePair) {
  101. //          final NameValuePair other = (NameValuePair) obj;
  102. //          return new EqualsBuilder().append(name, other.name).append(value,
  103. //                  other.value).isEquals();
  104. //
  105. //      }
  106. //      return false;
  107. //  }
  108. //
  109. //  @Override
  110. //  public int hashCode() {
  111. //      return new HashCodeBuilder().append(this.name).append(this.value)
  112. //              .toHashCode();
  113. //  }

  114.     @Override
  115.     public String toString() {
  116.         return new ToStringBuilder(this).append("name", name).append("value",
  117.                 value).toString();
  118.     }

  119. }