UserAgent.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 net.sf.qualitycheck.Check;

  20. /**
  21.  * {@code UserAgent} is an immutable entity that represents the informations about web-based client applications like
  22.  * Web browsers, search engines or crawlers (spiders) as well as mobile phones, screen readers and braille browsers.
  23.  *
  24.  * @author André Rouél
  25.  */
  26. public final class UserAgent implements ReadableUserAgent, Serializable {

  27.     public static final class Builder implements ReadableUserAgent {

  28.         private DeviceCategory deviceCategory = EMPTY.deviceCategory;

  29.         private UserAgentFamily family = EMPTY.family;

  30.         private String icon = EMPTY.icon;

  31.         private String name = EMPTY.name;

  32.         private OperatingSystem operatingSystem = OperatingSystem.EMPTY;

  33.         private String producer = EMPTY.producer;

  34.         private String producerUrl = EMPTY.producerUrl;

  35.         private UserAgentType type = EMPTY.type;

  36.         private String typeName = EMPTY.typeName;

  37.         private String url = EMPTY.url;

  38.         private String userAgentString = "";

  39.         private VersionNumber versionNumber = VersionNumber.UNKNOWN;

  40.         public Builder() {
  41.             // default constructor
  42.         }

  43.         public Builder(@Nonnull final String userAgentString) {
  44.             Check.notNull(userAgentString, "userAgentString");
  45.             this.userAgentString = userAgentString;
  46.         }

  47.         @Nonnull
  48.         public UserAgent build() {
  49.             return new UserAgent(deviceCategory, family, icon, name, operatingSystem, producer, producerUrl, type, typeName, url,
  50.                     versionNumber);
  51.         }

  52.         @Override
  53.         public DeviceCategory getDeviceCategory() {
  54.             return deviceCategory;
  55.         }

  56.         @Override
  57.         public UserAgentFamily getFamily() {
  58.             return family;
  59.         }

  60.         @Override
  61.         public String getIcon() {
  62.             return icon;
  63.         }

  64.         @Override
  65.         public String getName() {
  66.             return name;
  67.         }

  68.         @Override
  69.         public OperatingSystem getOperatingSystem() {
  70.             return operatingSystem;
  71.         }

  72.         @Override
  73.         public String getProducer() {
  74.             return producer;
  75.         }

  76.         @Override
  77.         public String getProducerUrl() {
  78.             return producerUrl;
  79.         }

  80.         @Override
  81.         public UserAgentType getType() {
  82.             return type;
  83.         }

  84.         @Override
  85.         public String getTypeName() {
  86.             return typeName;
  87.         }

  88.         @Override
  89.         public String getUrl() {
  90.             return url;
  91.         }

  92.         public String getUserAgentString() {
  93.             return userAgentString;
  94.         }

  95.         @Override
  96.         public VersionNumber getVersionNumber() {
  97.             return versionNumber;
  98.         }

  99.         @Nonnull
  100.         public Builder setDeviceCategory(@Nonnull final DeviceCategory deviceCategory) {
  101.             Check.notNull(deviceCategory, "deviceCategory");
  102.             this.deviceCategory = deviceCategory;
  103.             return this;
  104.         }

  105.         @Nonnull
  106.         public Builder setFamily(@Nonnull final UserAgentFamily family) {
  107.             Check.notNull(family, "family");
  108.             this.family = family;
  109.             return this;
  110.         }

  111.         @Nonnull
  112.         public Builder setIcon(@Nonnull final String icon) {
  113.             Check.notNull(icon, "icon");
  114.             this.icon = icon;
  115.             return this;
  116.         }

  117.         @Nonnull
  118.         public Builder setName(@Nonnull final String name) {
  119.             Check.notNull(name, "name");
  120.             this.name = name;
  121.             return this;
  122.         }

  123.         @Nonnull
  124.         public Builder setOperatingSystem(@Nonnull final OperatingSystem operatingSystem) {
  125.             Check.notNull(operatingSystem, "operatingSystem");
  126.             this.operatingSystem = operatingSystem;
  127.             return this;
  128.         }

  129.         @Nonnull
  130.         public Builder setOperatingSystem(@Nonnull final ReadableOperatingSystem os) {
  131.             Check.notNull(os, "os");
  132.             this.operatingSystem = new OperatingSystem(os.getFamily(), os.getFamilyName(), os.getIcon(), os.getName(), os.getProducer(),
  133.                     os.getProducerUrl(), os.getUrl(), os.getVersionNumber());
  134.             return this;
  135.         }

  136.         @Nonnull
  137.         public Builder setProducer(@Nonnull final String producer) {
  138.             Check.notNull(producer, "producer");
  139.             this.producer = producer;
  140.             return this;
  141.         }

  142.         @Nonnull
  143.         public Builder setProducerUrl(@Nonnull final String producerUrl) {
  144.             Check.notNull(producerUrl, "producerUrl");
  145.             this.producerUrl = producerUrl;
  146.             return this;
  147.         }

  148.         @Nonnull
  149.         public Builder setType(@Nonnull final UserAgentType type) {
  150.             Check.notNull(type, "type");
  151.             this.type = type;
  152.             this.typeName = type.getName();
  153.             return this;
  154.         }

  155.         @Nonnull
  156.         public Builder setTypeName(@Nonnull final String typeName) {
  157.             Check.notNull(typeName, "typeName");
  158.             this.type = UserAgentType.evaluateByTypeName(typeName);
  159.             this.typeName = typeName;
  160.             return this;
  161.         }

  162.         @Nonnull
  163.         public Builder setUrl(@Nonnull final String url) {
  164.             Check.notNull(url, "url");
  165.             this.url = url;
  166.             return this;
  167.         }

  168.         @Nonnull
  169.         public Builder setUserAgentString(@Nonnull final String userAgentString) {
  170.             Check.notNull(userAgentString, "userAgentString");
  171.             this.userAgentString = userAgentString;
  172.             return this;
  173.         }

  174.         @Nonnull
  175.         public Builder setVersionNumber(@Nonnull final VersionNumber versionNumber) {
  176.             Check.notNull(versionNumber, "versionNumber");
  177.             this.versionNumber = versionNumber;
  178.             return this;
  179.         }

  180.     }

  181.     public static final UserAgent EMPTY = new UserAgent(DeviceCategory.EMPTY, UserAgentFamily.UNKNOWN, "", "unknown",
  182.             OperatingSystem.EMPTY, "", "", UserAgentType.UNKNOWN, "", "", VersionNumber.UNKNOWN);

  183.     /**
  184.      * Serialization version
  185.      */
  186.     private static final long serialVersionUID = 1L;

  187.     @Nonnull
  188.     private final DeviceCategory deviceCategory;

  189.     @Nonnull
  190.     private final UserAgentFamily family;

  191.     @Nonnull
  192.     private final String icon;

  193.     @Nonnull
  194.     private final String name;

  195.     @Nonnull
  196.     private final OperatingSystem operatingSystem;

  197.     @Nonnull
  198.     private final String producer;

  199.     @Nonnull
  200.     private final String producerUrl;

  201.     @Nonnull
  202.     private final UserAgentType type;

  203.     @Nonnull
  204.     private final String typeName;

  205.     @Nonnull
  206.     private final String url;

  207.     @Nonnull
  208.     private final VersionNumber versionNumber;

  209.     public UserAgent(@Nonnull final DeviceCategory deviceType, @Nonnull final UserAgentFamily family, @Nonnull final String icon,
  210.             @Nonnull final String name, @Nonnull final OperatingSystem operatingSystem, @Nonnull final String producer,
  211.             @Nonnull final String producerUrl, @Nonnull final UserAgentType type, @Nonnull final String typeName,
  212.             @Nonnull final String url, @Nonnull final VersionNumber versionNumber) {
  213.         Check.notNull(deviceType, "deviceType");
  214.         Check.notNull(family, "family");
  215.         Check.notNull(icon, "icon");
  216.         Check.notNull(name, "name");
  217.         Check.notNull(operatingSystem, "operatingSystem");
  218.         Check.notNull(producer, "producer");
  219.         Check.notNull(producerUrl, "producerUrl");
  220.         Check.notNull(type, "type");
  221.         Check.notNull(typeName, "typeName");
  222.         Check.notNull(url, "url");
  223.         Check.notNull(versionNumber, "versionNumber");

  224.         this.deviceCategory = deviceType;
  225.         this.family = family;
  226.         this.icon = icon;
  227.         this.name = name;
  228.         this.operatingSystem = operatingSystem;
  229.         this.producer = producer;
  230.         this.producerUrl = producerUrl;
  231.         this.type = type;
  232.         this.typeName = typeName;
  233.         this.url = url;
  234.         this.versionNumber = versionNumber;
  235.     }

  236.     @Override
  237.     public boolean equals(final Object obj) {
  238.         if (this == obj) {
  239.             return true;
  240.         }
  241.         if (obj == null) {
  242.             return false;
  243.         }
  244.         if (getClass() != obj.getClass()) {
  245.             return false;
  246.         }
  247.         final UserAgent other = (UserAgent) obj;
  248.         if (deviceCategory != other.deviceCategory) {
  249.             return false;
  250.         }
  251.         if (!family.equals(other.family)) {
  252.             return false;
  253.         }
  254.         if (!icon.equals(other.icon)) {
  255.             return false;
  256.         }
  257.         if (!name.equals(other.name)) {
  258.             return false;
  259.         }
  260.         if (!operatingSystem.equals(other.operatingSystem)) {
  261.             return false;
  262.         }
  263.         if (!producer.equals(other.producer)) {
  264.             return false;
  265.         }
  266.         if (!producerUrl.equals(other.producerUrl)) {
  267.             return false;
  268.         }
  269.         if (!type.equals(other.type)) {
  270.             return false;
  271.         }
  272.         if (!typeName.equals(other.typeName)) {
  273.             return false;
  274.         }
  275.         if (!url.equals(other.url)) {
  276.             return false;
  277.         }
  278.         if (!versionNumber.equals(other.versionNumber)) {
  279.             return false;
  280.         }
  281.         return true;
  282.     }

  283.     @Nonnull
  284.     @Override
  285.     public DeviceCategory getDeviceCategory() {
  286.         return deviceCategory;
  287.     }

  288.     @Override
  289.     public UserAgentFamily getFamily() {
  290.         return family;
  291.     }

  292.     @Override
  293.     public String getIcon() {
  294.         return icon;
  295.     }

  296.     @Override
  297.     public String getName() {
  298.         return name;
  299.     }

  300.     @Override
  301.     public OperatingSystem getOperatingSystem() {
  302.         return operatingSystem;
  303.     }

  304.     @Override
  305.     public String getProducer() {
  306.         return producer;
  307.     }

  308.     @Override
  309.     public String getProducerUrl() {
  310.         return producerUrl;
  311.     }

  312.     @Override
  313.     public UserAgentType getType() {
  314.         return type;
  315.     }

  316.     @Override
  317.     public String getTypeName() {
  318.         return typeName;
  319.     }

  320.     @Override
  321.     public String getUrl() {
  322.         return url;
  323.     }

  324.     @Override
  325.     public VersionNumber getVersionNumber() {
  326.         return versionNumber;
  327.     }

  328.     @Override
  329.     public int hashCode() {
  330.         final int prime = 31;
  331.         int result = 1;
  332.         result = prime * result + deviceCategory.hashCode();
  333.         result = prime * result + family.hashCode();
  334.         result = prime * result + icon.hashCode();
  335.         result = prime * result + name.hashCode();
  336.         result = prime * result + operatingSystem.hashCode();
  337.         result = prime * result + producer.hashCode();
  338.         result = prime * result + producerUrl.hashCode();
  339.         result = prime * result + type.hashCode();
  340.         result = prime * result + typeName.hashCode();
  341.         result = prime * result + url.hashCode();
  342.         result = prime * result + versionNumber.hashCode();
  343.         return result;
  344.     }

  345.     @Nonnull
  346.     @Override
  347.     public String toString() {
  348.         final StringBuilder builder = new StringBuilder();
  349.         builder.append("UserAgent [deviceCategory=");
  350.         builder.append(deviceCategory);
  351.         builder.append(", family=");
  352.         builder.append(family);
  353.         builder.append(", icon=");
  354.         builder.append(icon);
  355.         builder.append(", name=");
  356.         builder.append(name);
  357.         builder.append(", operatingSystem=");
  358.         builder.append(operatingSystem);
  359.         builder.append(", producer=");
  360.         builder.append(producer);
  361.         builder.append(", producerUrl=");
  362.         builder.append(producerUrl);
  363.         builder.append(", type=");
  364.         builder.append(type);
  365.         builder.append(", typeName=");
  366.         builder.append(typeName);
  367.         builder.append(", url=");
  368.         builder.append(url);
  369.         builder.append(", versionNumber=");
  370.         builder.append(versionNumber);
  371.         builder.append("]");
  372.         return builder.toString();
  373.     }

  374. }