All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.batik.parser.FloatArrayProducer Maven / Gradle / Ivy

There is a newer version: 1.2.2.1-jre17
Show newest version
/*

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You 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 org.apache.batik.parser;

import java.util.LinkedList;

/**
 * A handler class that generates an array of floats from parsing a
 * number list or a point list.
 *
 * @author Cameron McCormack
 * @version $Id: FloatArrayProducer.java 1804130 2017-08-04 14:41:11Z ssteiner $
 */
public class FloatArrayProducer
        extends DefaultNumberListHandler
        implements PointsHandler {

    /**
     * List of float[] objects.
     */
    protected LinkedList as;

    /**
     * The current float[] object.
     */
    protected float[] a;

    /**
     * The index in which to store the next number.
     */
    protected int index;

    /**
     * The total number of floats accumulated.
     */
    protected int count;

    /**
     * Returns the array of floats accumulated.
     */
    public float[] getFloatArray() {
        return a;
    }

    // NumberListHandler /////////////////////////////////////////////////////

    /**
     * Invoked when the number list attribute starts.
     * @exception ParseException if an error occures while processing the
     *                           number list.
     */
    public void startNumberList() throws ParseException {
        as = new LinkedList();
        a = new float[11];
        count = 0;
        index = 0;
    }

    /**
     * Invoked when a float value has been parsed.
     * @exception ParseException if an error occures while processing
     *                           the number
     */
    public void numberValue(float v) throws ParseException {
        if (index == a.length) {
            as.add(a);
            a = new float[a.length * 2 + 1];
            index = 0;
        }
        a[index++] = v;
        count++;
    }

    /**
     * Invoked when the number list attribute ends.
     * @exception ParseException if an error occures while processing the
     *                           number list.
     */
    public void endNumberList() throws ParseException {
        float[] all = new float[count];
        int pos = 0;
        for (Object a1 : as) {
            float[] b = (float[]) a1;
            System.arraycopy(b, 0, all, pos, b.length);
            pos += b.length;
        }
        System.arraycopy(a, 0, all, pos, index);
        as.clear();
        a = all;
    }

    // PointsHandler /////////////////////////////////////////////////////////

    /**
     * Implements {@link PointsHandler#startPoints()}.
     */
    public void startPoints() throws ParseException {
        startNumberList();
    }

    /**
     * Implements {@link PointsHandler#point(float,float)}.
     */
    public void point(float x, float y) throws ParseException {
        numberValue(x);
        numberValue(y);
    }

    /**
     * Implements {@link PointsHandler#endPoints()}.
     */
    public void endPoints() throws ParseException {
        endNumberList();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy