001/* 002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved. 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 implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.tribuo.data.columnar.extractors; 018 019import com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance; 020import com.oracle.labs.mlrg.olcut.provenance.impl.ConfiguredObjectProvenanceImpl; 021 022import java.util.Optional; 023import java.util.logging.Logger; 024 025/** 026 * Extracts the field value and emits it as a String. 027 */ 028public class IdentityExtractor extends SimpleFieldExtractor<String> { 029 private static final Logger logger = Logger.getLogger(IdentityExtractor.class.getName()); 030 031 /** 032 * Extracts the String value from the supplied field. 033 * Writes the metadata out using the field name as the key. 034 * @param fieldName The field name to inspect. 035 */ 036 public IdentityExtractor(String fieldName) { 037 super(fieldName); 038 } 039 040 /** 041 * Extracts the String value from the supplied field. 042 * Writes the metadata out using the metadataName as the key. 043 * @param fieldName The field name to inspect. 044 * @param metadataName The metadata name to emit. 045 */ 046 public IdentityExtractor(String fieldName, String metadataName) { 047 super(fieldName, metadataName); 048 } 049 050 /** 051 * For olcut. 052 */ 053 private IdentityExtractor() {} 054 055 @Override 056 public Class<String> getValueType() { 057 return String.class; 058 } 059 060 @Override 061 protected Optional<String> extractField(String value) { 062 if (value == null || value.isEmpty()) { 063 return Optional.empty(); 064 } else { 065 return Optional.of(value); 066 } 067 } 068 069 @Override 070 public ConfiguredObjectProvenance getProvenance() { 071 return new ConfiguredObjectProvenanceImpl(this,"FieldExtractor"); 072 } 073}