001/*
002 * Copyright 2021 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.dwc.terms;
017
018import java.io.Serializable;
019import java.net.URI;
020
021public class BibTexTerm implements Term, Serializable {
022
023  public static final String PREFIX = "bib";
024  public static final String NS = "http://bibtex.org/";
025  public static final URI NS_URI = URI.create(NS);
026
027  public static final Term CLASS_TERM = new BibTexTerm("BibTeX", true);
028
029  private final String name;
030  private final boolean isClass;
031
032  public static BibTexTerm buildFromURI(String uri) {
033    if (uri == null || !uri.startsWith(NS) || uri.equalsIgnoreCase(NS)) {
034      throw new IllegalArgumentException("The qualified name URI is required and must be in the bibtex.org domain");
035    }
036    String name = uri.replaceFirst(NS, "");
037    return new BibTexTerm(name, false);
038  }
039
040  public static BibTexTerm buildFromPrefix(String prefixedTerm) {
041    if (prefixedTerm == null || !prefixedTerm.startsWith(PREFIX+":")) {
042      throw new IllegalArgumentException("The prefixed name is required and must start with " + PREFIX);
043    }
044    String name = prefixedTerm.replaceFirst(PREFIX+":", "");
045    return new BibTexTerm(name, false);
046  }
047
048  public BibTexTerm(String name) {
049    this(name, false);
050  }
051
052  private BibTexTerm(String name, boolean isClass) {
053    this.name = name;
054    this.isClass = isClass;
055  }
056
057  @Override
058  public String simpleName() {
059    return name;
060  }
061
062  @Override
063  public boolean isClass() {
064    return isClass;
065  }
066
067  @Override
068  public String toString() {
069    return prefixedName();
070  }
071
072  @Override
073  public boolean equals(Object o) {
074    if (this == o) return true;
075    if (!(o instanceof Term)) return false;
076
077    Term that = (Term) o;
078
079    return qualifiedName().equals(that.qualifiedName());
080  }
081
082  @Override
083  public int hashCode() {
084    return qualifiedName().hashCode();
085  }
086
087  @Override
088  public String prefix() {
089    return PREFIX;
090  }
091
092  @Override
093  public URI namespace() {
094    return NS_URI;
095  }
096}