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

com.fasterxml.jackson.jr.ob.comp.CollectionComposer Maven / Gradle / Ivy

Go to download

Simple data-binding that builds directly on jackson-core (streaming), has no other dependencies, and provides additional builder-style content generator

There is a newer version: 2.18.2
Show newest version
package com.fasterxml.jackson.jr.ob.comp;

import java.util.*;

import com.fasterxml.jackson.core.JsonGenerator;

public class CollectionComposer>
    extends ComposerBase
{
    protected final PARENT _parent;

    protected C _collection;
    
    public CollectionComposer(PARENT parent) {
        super();
        _parent = parent;
    }

    public CollectionComposer(C coll) {
        super();
        _parent = null;
        _collection = coll;
    }

    public static > CollectionComposer
    rootComposer(T coll) {
        return new CollectionComposer(coll);
    }
    
    /*
    /**********************************************************************
    /* Abstract method impls
    /**********************************************************************
     */

    @Override
    public void flush()  { }
    
    @Override
    protected CollectionComposer _start() {
        if (_collection == null) {
            _collection = constructCollection();
        }
        return this;
    }

    @Override
    protected C _finish() {
        if (_open) {
            _open = false;
        }
        return _collection;
    }

    /*
    /**********************************************************************
    /* Compose methods, structures
    /**********************************************************************
     */

    public CollectionComposer,?> startArray()
    {
        _closeChild();
        CollectionComposer,?> child = _startCollection(this);
        _collection.add(child._collection);
        return child;
    }

    public MapComposer> startObject()
    {
        _closeChild();
        MapComposer> child = _startMap(this);
        _collection.add(child._map);
        return child;
    }

    public C finish() {
        return _finish();
    }
    
    /*
    /**********************************************************************
    /* Compose methods, scalars, number
    /**********************************************************************
     */

    public CollectionComposer add(int value)
    {
        _collection.add(Integer.valueOf(value));
        return this;
    }

    public CollectionComposer add(long value)
    {
        _collection.add(Long.valueOf(value));
        return this;
    }

    public CollectionComposer add(double value)
    {
        _collection.add(Double.valueOf(value));
        return this;
    }
    
    /*
    /**********************************************************************
    /* Compose methods, scalars, textual / binary
    /**********************************************************************
     */

    public CollectionComposer add(String value)
    {
        _collection.add(value);
        return this;
    }

    public CollectionComposer add(CharSequence value)
    {
        String str = (value == null) ? null : value.toString();
        _collection.add(str);
        return this;
    }

    /*
    /**********************************************************************
    /* Compose methods, scalars, other
    /**********************************************************************
     */

    public CollectionComposer addNull()
    {
        _collection.add(null);
        return this;
    }

    public CollectionComposer add(boolean value)
    {
        _collection.add(value ? Boolean.TRUE : Boolean.FALSE);
        return this;
    }
    
    /**
     * Method used to add Java Object ("POJO") into sequence being
     * composed: this requires that the underlying {@link JsonGenerator}
     * has a properly configured {@link com.fasterxml.jackson.core.ObjectCodec}
     * to use for serializer object.
     */
    public CollectionComposer addObject(Object pojo)
    {
        _collection.add(pojo);
        return this;
    }

    /*
    /**********************************************************************
    /* Compose methods, structures
    /**********************************************************************
     */
    
    public PARENT end()
    {
        _closeChild();
        if (_open) {
            _open = false;
            _parent._childClosed();
        }
        return _parent;
    }

    /*
    /**********************************************************************
    /* Overridable helper methods
    /**********************************************************************
     */
    
    @SuppressWarnings("unchecked")
    protected C constructCollection() {
        return (C) new ArrayList();
    }

    /*
    /**********************************************************************
    /* Internal helper methods
    /**********************************************************************
     */

    protected void _closeChild()
    {
        if (_child != null) {
            Object value = _child._safeFinish();
            _collection.add(value);
            _child = null;
        }
    }
}