001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime.reflect;
027:
028: import com.sun.xml.internal.bind.api.AccessorException;
029: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
030:
031: /**
032: * {@link Lister} for primitive type arrays.
033: *
034: * <p>
035: * B y t e ArrayLister is used as the master to generate the rest of the
036: * lister classes. Do not modify the generated copies.
037: */
038: final class PrimitiveArrayListerFloat<BeanT>
039: extends
040: Lister<BeanT, float[], Float, PrimitiveArrayListerFloat.FloatArrayPack> {
041:
042: private PrimitiveArrayListerFloat() {
043: }
044:
045: /*package*/static void register() {
046: Lister.primitiveArrayListers.put(Float.TYPE,
047: new PrimitiveArrayListerFloat());
048: }
049:
050: public ListIterator<Float> iterator(final float[] objects,
051: XMLSerializer context) {
052: return new ListIterator<Float>() {
053: int idx = 0;
054:
055: public boolean hasNext() {
056: return idx < objects.length;
057: }
058:
059: public Float next() {
060: return objects[idx++];
061: }
062: };
063: }
064:
065: public FloatArrayPack startPacking(BeanT current,
066: Accessor<BeanT, float[]> acc) {
067: return new FloatArrayPack();
068: }
069:
070: public void addToPack(FloatArrayPack objects, Float o) {
071: objects.add(o);
072: }
073:
074: public void endPacking(FloatArrayPack pack, BeanT bean,
075: Accessor<BeanT, float[]> acc) throws AccessorException {
076: acc.set(bean, pack.build());
077: }
078:
079: public void reset(BeanT o, Accessor<BeanT, float[]> acc)
080: throws AccessorException {
081: acc.set(o, new float[0]);
082: }
083:
084: static final class FloatArrayPack {
085: float[] buf = new float[16];
086: int size;
087:
088: void add(Float b) {
089: if (buf.length == size) {
090: // realloc
091: float[] nb = new float[buf.length * 2];
092: System.arraycopy(buf, 0, nb, 0, buf.length);
093: buf = nb;
094: }
095: buf[size++] = b;
096: }
097:
098: float[] build() {
099: if (buf.length == size)
100: // if we are lucky enough
101: return buf;
102:
103: float[] r = new float[size];
104: System.arraycopy(buf, 0, r, 0, size);
105: return r;
106: }
107: }
108: }
|