com.tectonica.jonix.JonixRecord Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jonix Show documentation
Show all versions of jonix Show documentation
Commercial-grade library for extracting data from ONIX sources
/*
* Copyright (C) 2012-2024 Zach Melamed
*
* Latest version available online at https://github.com/zach-m/jonix
* Contact me at [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tectonica.jonix;
import com.tectonica.jonix.common.OnixProduct;
import java.util.Map;
/**
* This class is generated by {@link JonixRecords} when iterating over ONIX source, and basically wraps an ONIX Product
* with some contextual information. It contains the {@link JonixSource} from which the ONIX Product was read, which,
* among others, contains the ONIX Header of that source.
*
* Note that the ONIX Product object contained in this class is a low-level {@link OnixProduct} which requires casting
* to an actual descendant in order to access the actual product content. For example:
*
* void foo(JonixRecord record) {
* if (record.product instanceof com.tectonica.jonix.onix3.Product) {
* com.tectonica.jonix.onix3.Product product3 = (com.tectonica.jonix.onix3.Product) record.product;
* // TODO: this is an Onix3 <Product>, do something with 'product3' hereafter
* } else if (record.product instanceof com.tectonica.jonix.onix2.Product) {
* com.tectonica.jonix.onix2.Product product2 = (com.tectonica.jonix.onix2.Product) record.product;
* // TODO: this is an Onix2 <Product>, do something with 'product2' hereafter
* } else {
* throw new IllegalArgumentException();
* }
* }
*
* Alternatively, it is possible to use {@link com.tectonica.jonix.unify.JonixUnifier} in order transform the raw
* {@link OnixProduct} into a {@link com.tectonica.jonix.unify.base.BaseProduct} which is easier to manipulate:
*
* void foo(JonixRecord record) {
* BaseProduct baseProduct = JonixUnifier.unifyProduct(record.product);
* // TODO: access the content of 'baseProduct'
* }
*
*/
public class JonixRecord {
protected final Map globalConfig;
public final JonixSource source;
public final OnixProduct product;
protected JonixRecord(Map globalConfig, JonixSource source, OnixProduct product) {
assert globalConfig != null;
assert source != null;
assert product != null;
this.globalConfig = globalConfig;
this.source = source;
this.product = product;
}
/**
* call this method to stop streaming products from the current source, and move on to the next one (if listed)
*/
public void breakCurrentSource() {
store("jonix.source.break", true);
}
/**
* call this method to stop streaming products from current as well as the next sources (if listed)
*/
public void breakStream() {
store("jonix.stream.break", true);
}
/**
* Stores an object for later use during the streaming process. The stored object can be retrieved with
* {@link #retrieve(String)}.
*/
public JonixRecord store(String key, T value) {
globalConfig.put(key, value);
return this;
}
/**
* @return an object stored with {@link #store(String, Object)} during the streaming, or {@code null} if the
* {@code key} doesn't exist
*/
public T retrieve(String key) {
return (T) globalConfig.get(key);
}
/**
* @return an object stored with {@link #store(String, Object)} during the streaming, or {@code defaultValue} if the
* {@code key} doesn't exist
*/
public T retrieve(String key, T defaultValue) {
return (T) globalConfig.getOrDefault(key, defaultValue);
}
}