All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.model.http.useragent.UserAgentInfo Maven / Gradle / Ivy

There is a newer version: 1.1.5
Show newest version
package com.litongjava.model.http.useragent;

import java.util.regex.Pattern;

/**
 * User-agent信息
 * 
 * @author looly
 * @since 4.2.1
 */
public class UserAgentInfo {

  public static final String NameUnknown = "Unknown";

  /** 信息名称 */
  private String name;
  /** 信息匹配模式 */
  private Pattern pattern;

  /**
   * 构造
   * 
   * @param name 名字
   * @param regex 表达式
   */
  public UserAgentInfo(String name, String regex) {
    this(name, (null == regex) ? null : Pattern.compile(regex, Pattern.CASE_INSENSITIVE));
  }

  /**
   * 构造
   * 
   * @param name 名字
   * @param pattern 匹配模式
   */
  public UserAgentInfo(String name, Pattern pattern) {
    this.name = name;
    this.pattern = pattern;
  }

  /**
   * 获取信息名称
   * 
   * @return 信息名称
   */
  public String getName() {
    return name;
  }

  /**
   * 获取匹配模式
   * 
   * @return 匹配模式
   */
  public Pattern getPattern() {
    return pattern;
  }

  /**
   * 指定内容中是否包含匹配此信息的内容
   * 
   * @param content User-Agent字符串
   * @return 是否包含匹配此信息的内容
   */
  public boolean isMatch(String content) {
    return contains(this.pattern, content);
  }

  private boolean contains(Pattern pattern, String content) {
    if (null == pattern || null == content) {
      return false;
    }
    return pattern.matcher(content).find();
  }

  /**
   * 是否为Unknown
   * 
   * @return 是否为Unknown
   */
  public boolean isUnknown() {
    return NameUnknown.equals(this.name);
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final UserAgentInfo other = (UserAgentInfo) obj;
    if (name == null) {
      if (other.name != null) {
        return false;
      }
    } else if (!name.equals(other.name)) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return this.name;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy