CustomFieldValue.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.CompareToBuilder;
  20. import org.apache.commons.lang.builder.ToStringBuilder;

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

  23. /**
  24.  * An option for the value of a CustomField of type <code>LIST</code>.
  25.  *
  26.  * @author ready
  27.  * @author johnny
  28.  */
  29. public class CustomFieldValue extends AbstractEntity {

  30.     /**
  31.      *
  32.      */
  33.     private static final long serialVersionUID = 1L;
  34.     public static final Comparator<CustomFieldValue> SORT_ORDER_COMPARATOR = new SortOrderComparator();

  35.     /**
  36.      * The custom field to which this option belongs.
  37.      */
  38.     private CustomField customField;


  39.     /**
  40.      * This option's value.
  41.      */
  42.     private String value;

  43.     /**
  44.      * This option's order among all available options for the
  45.      * <code>customField</code>.
  46.      */
  47.     private int sortOrder;

  48.     /**
  49.      * Default constructor (required by Hibernate).
  50.      * <p/>
  51.      * <p>
  52.      * PENDING: should be <code>private</code> so that it can only be used by
  53.      * Hibernate, to ensure that the fields which form an instance's identity
  54.      * are always initialized/never <tt>null</tt>.
  55.      * </p>
  56.      */
  57.     public CustomFieldValue() {
  58.     }

  59.     public CustomFieldValue(CustomField customField, String value) {
  60.         setCustomField(customField);
  61.         setValue(value);
  62.     }

  63.     public CustomField getCustomField() {
  64.         return (customField);
  65.     }

  66.     public void setCustomField(CustomField customField) {
  67.         if (customField == null) {
  68.             throw new IllegalArgumentException("null customField");
  69.         }
  70.         this.customField = customField;
  71.     }


  72.     public String getValue() {
  73.         return value;
  74.     }

  75.     /**
  76.      * @param value
  77.      */
  78.     public void setValue(String value) {
  79.         if (value == null) {
  80.             throw new IllegalArgumentException("null value");
  81.         }
  82.         this.value = value;
  83.     }

  84.     public int getSortOrder() {
  85.         return sortOrder;
  86.     }

  87.     public void setSortOrder(int sortOrder) {
  88.         this.sortOrder = sortOrder;
  89.     }

  90.     /**
  91.      * Returns a string with this instance's id and natural key.
  92.      */
  93.     @Override
  94.     public String toString() {
  95.         return new ToStringBuilder(this).append("id", getId()).append(
  96.                 "customField", getCustomField()).append("value", getValue())
  97.                 .toString();
  98.     }

  99.     /**
  100.      * Compares 2 CustomFieldValues by custom field and sort order.
  101.      * <p/>
  102.      * <p>
  103.      * Note that it doesn't match the class' natural ordering because it doesn't
  104.      * take into account the custom field. <br>
  105.      * It should therefore only be used to compare options that belong to a
  106.      * single custom field.
  107.      * </p>
  108.      */
  109.     private static class SortOrderComparator implements
  110.             Comparator<CustomFieldValue>, Serializable {
  111.         /**
  112.          *
  113.          */
  114.         private static final long serialVersionUID = 1L;

  115.         public int compare(CustomFieldValue a, CustomFieldValue b) {
  116.             return new CompareToBuilder().append(a.getSortOrder(),
  117.                     b.getSortOrder())
  118.                     .toComparison();
  119.         }

  120.     }


  121. }