DeviceCategory.java

  1. /*******************************************************************************
  2.  * Copyright 2012 André Rouél
  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. package net.sf.uadetector;

  17. import java.io.Serializable;

  18. import javax.annotation.Nonnull;
  19. import javax.annotation.concurrent.Immutable;
  20. import javax.annotation.concurrent.NotThreadSafe;

  21. import net.sf.qualitycheck.Check;

  22. @Immutable
  23. public final class DeviceCategory implements ReadableDeviceCategory, Serializable {

  24.     @NotThreadSafe
  25.     public static final class Builder {

  26.         private Category category;

  27.         private String icon;

  28.         private String infoUrl;

  29.         private String name;

  30.         public Builder() {
  31.             // default constructor
  32.         }

  33.         public Builder(@Nonnull final DeviceCategory deviceCategory) {
  34.             Check.notNull(deviceCategory, "deviceCategory");
  35.             category = Check.notNull(deviceCategory.getCategory(), "deviceCategory.getCategory()");
  36.             icon = Check.notNull(deviceCategory.getIcon(), "deviceCategory.getIcon()");
  37.             infoUrl = Check.notNull(deviceCategory.getInfoUrl(), "deviceCategory.getInfoUrl()");
  38.             name = Check.notNull(deviceCategory.getName(), "deviceCategory.getName()");
  39.         }

  40.         @Nonnull
  41.         public DeviceCategory build() {
  42.             return new DeviceCategory(category, icon, infoUrl, name);
  43.         }

  44.         @Nonnull
  45.         public Builder setCategory(@Nonnull final Category category) {
  46.             this.category = Check.notNull(category, "category");
  47.             return this;
  48.         }

  49.         @Nonnull
  50.         public Builder setIcon(@Nonnull final String icon) {
  51.             this.icon = Check.notNull(icon, "icon");
  52.             return this;
  53.         }

  54.         @Nonnull
  55.         public Builder setInfoUrl(@Nonnull final String infoUrl) {
  56.             this.infoUrl = Check.notNull(infoUrl, "infoUrl");
  57.             return this;
  58.         }

  59.         @Nonnull
  60.         public Builder setName(@Nonnull final String name) {
  61.             this.name = Check.notNull(name, "name");
  62.             return this;
  63.         }

  64.     }

  65.     private static final long serialVersionUID = 1L;

  66.     /**
  67.      * Represents a not set device category.
  68.      */
  69.     public static final DeviceCategory EMPTY = new DeviceCategory();

  70.     private static int buildHashCode(@Nonnull final Category category, @Nonnull final String icon, @Nonnull final String infoUrl,
  71.             @Nonnull final String name) {
  72.         final int prime = 31;
  73.         int result = 1;
  74.         result = prime * result + category.hashCode();
  75.         result = prime * result + icon.hashCode();
  76.         result = prime * result + infoUrl.hashCode();
  77.         result = prime * result + name.hashCode();
  78.         return result;
  79.     }

  80.     @Nonnull
  81.     private final Category category;

  82.     @Nonnull
  83.     private final String icon;

  84.     @Nonnull
  85.     private final String infoUrl;

  86.     @Nonnull
  87.     private final String name;

  88.     private final int hash;

  89.     /**
  90.      * Builds an instance that represents an empty device category.
  91.      * <p>
  92.      * <b>Attention</b>: This is only intended to build one instance at runtime to represent value behind the constant
  93.      * {@link #EMPTY}.
  94.      */
  95.     private DeviceCategory() {
  96.         category = Category.UNKNOWN;
  97.         icon = "";
  98.         infoUrl = "";
  99.         name = "";
  100.         hash = buildHashCode(category, icon, infoUrl, name);
  101.     }

  102.     public DeviceCategory(@Nonnull final Category category, @Nonnull final String icon, @Nonnull final String infoUrl,
  103.             @Nonnull final String name) {
  104.         this.category = Check.notNull(category, "category");
  105.         this.icon = Check.notNull(icon, "icon");
  106.         this.infoUrl = Check.notNull(infoUrl, "infoUrl");
  107.         this.name = Check.notEmpty(name, "name");
  108.         hash = buildHashCode(category, icon, infoUrl, name);
  109.     }

  110.     @Override
  111.     public boolean equals(final Object obj) {
  112.         if (this == obj) {
  113.             return true;
  114.         }
  115.         if (obj == null) {
  116.             return false;
  117.         }
  118.         if (getClass() != obj.getClass()) {
  119.             return false;
  120.         }
  121.         final DeviceCategory other = (DeviceCategory) obj;
  122.         if (!category.equals(other.category)) {
  123.             return false;
  124.         }
  125.         if (!icon.equals(other.icon)) {
  126.             return false;
  127.         }
  128.         if (!infoUrl.equals(other.infoUrl)) {
  129.             return false;
  130.         }
  131.         if (!name.equals(other.name)) {
  132.             return false;
  133.         }
  134.         return true;
  135.     }

  136.     @Override
  137.     @Nonnull
  138.     public Category getCategory() {
  139.         return category;
  140.     }

  141.     @Override
  142.     @Nonnull
  143.     public String getIcon() {
  144.         return icon;
  145.     }

  146.     @Override
  147.     @Nonnull
  148.     public String getInfoUrl() {
  149.         return infoUrl;
  150.     }

  151.     @Override
  152.     @Nonnull
  153.     public String getName() {
  154.         return name;
  155.     }

  156.     @Override
  157.     public int hashCode() {
  158.         return hash;
  159.     }

  160.     @Override
  161.     public String toString() {
  162.         return "DeviceCategory [category=" + category + ", icon=" + icon + ", infoUrl=" + infoUrl + ", name=" + name + "]";
  163.     }

  164. }