    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.lang3.text.translate;
    018
    019import java.io.IOException;
    020import java.io.Writer;
    021
    022import org.apache.commons.lang3.ArrayUtils;
    023
    024/**
    025 * Executes a sequence of translators one after the other. Execution ends whenever
    026 * the first translator consumes code points from the input.
    027 *
    028 * @since 3.0
    029 * @deprecated As of 3.6, use Apache Commons Text
    030 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/AggregateTranslator.html">
    031 * AggregateTranslator</a> instead
    032 */
    033@Deprecated
    034public class AggregateTranslator extends CharSequenceTranslator {
    035
    036    private final CharSequenceTranslator[] translators;
    037
    038    /**
    039     * Specify the translators to be used at creation time.
    040     *
    041     * @param translators CharSequenceTranslator array to aggregate
    042     */
    043    public AggregateTranslator(final CharSequenceTranslator... translators) {
    044        this.translators = ArrayUtils.clone(translators);
    045    }
    046
    047    /**
    048     * The first translator to consume code points from the input is the 'winner'.
    049     * Execution stops with the number of consumed code points being returned.
    050     * {@inheritDoc}
    051     */
    052    @Override
    053    public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
    054        for (final CharSequenceTranslator translator : translators) {
    055            final int consumed = translator.translate(input, index, out);
    056            if (consumed != 0) {
    057                return consumed;
    058            }
    059        }
    060        return 0;
    061    }
    062
    063}