001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.common.parsers; 015 016import org.gbif.api.vocabulary.BasisOfRecord; 017import org.gbif.common.parsers.core.EnumParser; 018import org.gbif.common.parsers.core.ParseResult; 019 020import java.io.InputStream; 021import java.util.Optional; 022 023/** 024 * Singleton implementation of the dictionary that uses the file /dictionaries/parse/basisOfRecord.txt. 025 */ 026public class BasisOfRecordParser extends EnumParser<BasisOfRecord> { 027 028 private static BasisOfRecordParser singletonObject = null; 029 030 private BasisOfRecordParser(InputStream... file) { 031 super(BasisOfRecord.class, false, file); 032 } 033 034 /** 035 * Handles deprecations. 036 */ 037 private static BasisOfRecord getMappedValue(BasisOfRecord basisOfRecord) { 038 return 039 // Literature is replaced with Material Citation 040 (BasisOfRecord.LITERATURE == basisOfRecord ? BasisOfRecord.MATERIAL_CITATION : 041 // Unknown is replaced with Occurrence 042 (BasisOfRecord.UNKNOWN == basisOfRecord? BasisOfRecord.OCCURRENCE : null)); 043 } 044 045 public static BasisOfRecordParser getInstance() 046 throws ClassCastException, AbstractMethodError, ArithmeticException, ArrayIndexOutOfBoundsException { 047 synchronized (BasisOfRecordParser.class) { 048 if (singletonObject == null) { 049 singletonObject = new BasisOfRecordParser(BasisOfRecordParser.class.getResourceAsStream("/dictionaries/parse/basisOfRecord.tsv")); 050 } 051 } 052 return singletonObject; 053 } 054 055 @Override 056 public ParseResult<BasisOfRecord> parse(String input) { 057 ParseResult<BasisOfRecord> result = super.parse(input); 058 Optional<BasisOfRecord> mappedValue = Optional.ofNullable(result.getPayload()).map(BasisOfRecordParser::getMappedValue); 059 if (result.isSuccessful() && mappedValue.isPresent()) { 060 return ParseResult.success(result.getConfidence(), mappedValue.get()); 061 } 062 return result; 063 } 064}