Language.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.util.Comparator;

  22. /**
  23.  * Models a language entry.
  24.  *
  25.  * @author ready
  26.  */
  27. public class Language extends AbstractEntity {


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

  32.     private String locale;

  33.     private String resourceKey;

  34.     private String resourceValue;

  35.     /**
  36.      * Default constructor (required by Hibernate).
  37.      * <p/>
  38.      * <p>
  39.      * PENDING: should be <code>private</code> so that it can only be used by
  40.      * Hibernate, to ensure that the fields which form an instance's identity
  41.      * are always initialized/never <tt>null</tt>.
  42.      * </p>
  43.      */
  44.     public Language() {
  45.     }

  46.     public Language(String locale, String key) {
  47.         setLocale(locale);
  48.         setResourceKey(key);
  49.     }

  50.     /**
  51.      * Convenience constructor to set the value too.
  52.      */
  53.     public Language(String locale, String key, String value) {
  54.         this(locale, key);
  55.         setResourceValue(value);
  56.     }

  57.     public String getLocale() {
  58.         return locale;
  59.     }

  60.     public void setLocale(String locale) {
  61.         if (locale == null) {
  62.             throw new IllegalArgumentException("null locale");
  63.         }
  64.         this.locale = locale;
  65.     }

  66.     public String getResourceKey() {
  67.         return resourceKey;
  68.     }

  69.     public void setResourceKey(String resourceKey) {
  70.         if (resourceKey == null) {
  71.             throw new IllegalArgumentException("null resourceKey");
  72.         }
  73.         this.resourceKey = resourceKey;
  74.     }

  75.     public String getResourceValue() {
  76.         return resourceValue;
  77.     }

  78.     public void setResourceValue(String resourceValue) {
  79.         this.resourceValue = resourceValue;
  80.     }

  81.     // @Override
  82.     // public boolean equals(Object obj) {
  83.     // if (this == obj) {
  84.     // return true;
  85.     // }
  86.     //
  87.     // if (obj instanceof Language) {
  88.     // final Language other = (Language)obj;
  89.     //
  90.     // return this.resourceKey.equals(other.resourceKey)
  91.     // && this.locale.equals(other.locale);
  92.     // }
  93.     // return false;
  94.     // }
  95.     //
  96.     // @Override
  97.     // public int hashCode() {
  98.     // return this.resourceKey.hashCode() + this.locale.hashCode();
  99.     // }
  100.     //
  101.     @Override
  102.     public String toString() {
  103.         return new ToStringBuilder(this).append("id", getId()).append("resourceKey",
  104.                 getResourceKey()).append("locale", getLocale()).append("value", getResourceValue()).toString();
  105.     }

  106.     public static final Comparator<Language> KEY_COMPARATOR = new LanguageKeyComparator();

  107.     private static class LanguageKeyComparator implements Comparator<Language> {

  108.         public int compare(Language o1, Language o2) {
  109.             return new CompareToBuilder().append(o1.getResourceKey(), o2.getResourceKey()).append(o1.getLocale(), o2.getLocale()).append(o1.getId(), o2.getId()).toComparison();
  110.         }

  111.     }

  112.     public static final Comparator<Language> VALUE_COMPARATOR = new LanguageValueComparator();

  113.     private static class LanguageValueComparator implements Comparator<Language> {

  114.         public int compare(Language o1, Language o2) {
  115.             return new CompareToBuilder()
  116.                     .append(o1.getResourceValue(), o2.getResourceValue())
  117.                     .append(o1.getResourceKey(), o2.getResourceKey())
  118.                     .append(o1.getId(), o2.getId()).toComparison();
  119.         }

  120.     }

  121. }