001/*
002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     pierre
018 */
019package org.nuxeo.runtime.avro;
020
021import org.apache.avro.Schema;
022import org.nuxeo.lib.stream.codec.AvroSchemaStore;
023
024/**
025 * This service allows to create a {@link AvroSchemaFactoryContext}.
026 *
027 * @since 10.2
028 */
029public interface AvroService {
030
031    /**
032     * Gets the Schema store
033     *
034     * @since 10.3
035     */
036    AvroSchemaStore getSchemaStore();
037
038    /**
039     * Creates the Avro schema from an object.<br>
040     * <br>
041     * An AvroSchemaFactory handling the object class has to be implemented and registered to the AvroComponent..
042     *
043     * @param input any object
044     * @return the Avro schema
045     */
046    <D> Schema createSchema(D input);
047
048    /**
049     * Decodes a valid Avro name to its actual value.<br>
050     *
051     * @param input the name to decode
052     * @return the decoded name
053     */
054    String decodeName(String input);
055
056    /**
057     * Encodes a name for it to be eligible to Avro limitations (alphanumeric and _).<br>
058     * <br>
059     * By default Nuxeo can encode - and :<br>
060     * Other replacements can be registered to the AvroComponent.<br>
061     *
062     * @param input the name to encode
063     * @return the encoded name
064     */
065    String encodeName(String input);
066
067    /**
068     * Map an Avro data to an instance of the given class.<br>
069     * <br>
070     * An AvroMapper handling the given class has to be implemented and registered to the AvroComponent..
071     *
072     * @param schema the Avro schema
073     * @param clazz the class to map the Avro object to
074     * @param object the Avro data
075     * @return an instance of the given class
076     */
077    <D, M> D fromAvro(Schema schema, Class<D> clazz, M object);
078
079    /**
080     * Map an object to an Avro data.<br>
081     * <br>
082     * An AvroMapper handling the given class has to be implemented and registered.
083     *
084     * @param schema the Avro schema
085     * @param input the object to map to an Avro data
086     * @return the Avro data
087     */
088    <D, M> M toAvro(Schema schema, D input);
089
090}