public class

CLContainer

extends CLElement

 java.lang.Object

androidx.constraintlayout.core.parser.CLElement

↳androidx.constraintlayout.core.parser.CLContainer

Subclasses:

CLKey, CLArray, CLObject

Gradle dependencies

compile group: 'androidx.constraintlayout', name: 'constraintlayout-core', version: '1.1.0-beta01'

  • groupId: androidx.constraintlayout
  • artifactId: constraintlayout-core
  • version: 1.1.0-beta01

Artifact androidx.constraintlayout:constraintlayout-core:1.1.0-beta01 it located at Google repository (https://maven.google.com/)

Summary

Fields
from CLElementmContainer, mEnd, mStart, sBaseIndent, sMaxLine
Constructors
publicCLContainer(char[] content[])

Methods
public voidadd(CLElement element)

public static CLElementallocate(char[] content[])

public voidclear()

public CLContainerclone()

public booleanequals(java.lang.Object obj)

public CLElementget(int index)

public CLElementget(java.lang.String name)

public CLArraygetArray(int index)

public CLArraygetArray(java.lang.String name)

public CLArraygetArrayOrCreate(java.lang.String name)

public CLArraygetArrayOrNull(java.lang.String name)

public booleangetBoolean(int index)

public booleangetBoolean(java.lang.String name)

public floatgetFloat(int index)

public floatgetFloat(java.lang.String name)

public floatgetFloatOrNaN(java.lang.String name)

public intgetInt(int index)

public intgetInt(java.lang.String name)

public CLObjectgetObject(int index)

public CLObjectgetObject(java.lang.String name)

public CLObjectgetObjectOrNull(java.lang.String name)

public CLElementgetOrNull(int index)

public CLElementgetOrNull(java.lang.String name)

public java.lang.StringgetString(int index)

public java.lang.StringgetString(java.lang.String name)

public java.lang.StringgetStringOrNull(int index)

public java.lang.StringgetStringOrNull(java.lang.String name)

public booleanhas(java.lang.String name)

public inthashCode()

public java.util.ArrayList<java.lang.String>names()

public voidput(java.lang.String name, CLElement value)

public voidputNumber(java.lang.String name, float value)

public voidputString(java.lang.String name, java.lang.String value)

public voidremove(java.lang.String name)

public intsize()

public java.lang.StringtoString()

from CLElementaddIndent, content, getContainer, getDebugName, getEnd, getFloat, getInt, getLine, getStart, getStrClass, hasContent, isDone, isStarted, notStarted, setContainer, setEnd, setLine, setStart, toFormattedJSON, toJSON
from java.lang.Objectfinalize, getClass, notify, notifyAll, wait, wait, wait

Constructors

public CLContainer(char[] content[])

Methods

public static CLElement allocate(char[] content[])

public void add(CLElement element)

public java.lang.String toString()

public int size()

public java.util.ArrayList<java.lang.String> names()

public boolean has(java.lang.String name)

public void put(java.lang.String name, CLElement value)

public void putNumber(java.lang.String name, float value)

public void putString(java.lang.String name, java.lang.String value)

public void remove(java.lang.String name)

public void clear()

public CLElement get(java.lang.String name)

public int getInt(java.lang.String name)

public float getFloat(java.lang.String name)

public CLArray getArray(java.lang.String name)

public CLObject getObject(java.lang.String name)

public java.lang.String getString(java.lang.String name)

public boolean getBoolean(java.lang.String name)

public CLElement getOrNull(java.lang.String name)

public CLObject getObjectOrNull(java.lang.String name)

public CLArray getArrayOrNull(java.lang.String name)

public CLArray getArrayOrCreate(java.lang.String name)

public java.lang.String getStringOrNull(java.lang.String name)

public float getFloatOrNaN(java.lang.String name)

public CLElement get(int index)

public int getInt(int index)

public float getFloat(int index)

public CLArray getArray(int index)

public CLObject getObject(int index)

public java.lang.String getString(int index)

public boolean getBoolean(int index)

public CLElement getOrNull(int index)

public java.lang.String getStringOrNull(int index)

public CLContainer clone()

public boolean equals(java.lang.Object obj)

public int hashCode()

Source

/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed 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 androidx.constraintlayout.core.parser;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.Objects;

public class CLContainer extends CLElement {
    ArrayList<CLElement> mElements = new ArrayList<>();

    public CLContainer(char[] content) {
        super(content);
    }

    // @TODO: add description
    public static CLElement allocate(char[] content) {
        return new CLContainer(content);
    }

    // @TODO: add description
    public void add(CLElement element) {
        mElements.add(element);
        if (CLParser.sDebug) {
            System.out.println("added element " + element + " to " + this);
        }
    }

    @Override
    public String toString() {
        StringBuilder list = new StringBuilder();
        for (CLElement element : mElements) {
            if (list.length() > 0) {
                list.append("; ");
            }
            list.append(element);
        }
        return super.toString() + " = <" + list + " >";
    }

    // @TODO: add description
    public int size() {
        return mElements.size();
    }

    // @TODO: add description
    public ArrayList<String> names() {
        ArrayList<String> names = new ArrayList<>();
        for (CLElement element : mElements) {
            if (element instanceof CLKey) {
                CLKey key = (CLKey) element;
                names.add(key.content());
            }
        }
        return names;
    }

    // @TODO: add description
    public boolean has(String name) {
        for (CLElement element : mElements) {
            if (element instanceof CLKey) {
                CLKey key = (CLKey) element;
                if (key.content().equals(name)) {
                    return true;
                }
            }
        }
        return false;
    }

    // @TODO: add description
    public void put(String name, CLElement value) {
        for (CLElement element : mElements) {
            CLKey key = (CLKey) element;
            if (key.content().equals(name)) {
                key.set(value);
                return;
            }
        }
        CLKey key = (CLKey) CLKey.allocate(name, value);
        mElements.add(key);
    }

    // @TODO: add description
    public void putNumber(String name, float value) {
        put(name, new CLNumber(value));
    }

    public void putString(String name, String value) {
        CLElement stringElement = new CLString(value.toCharArray());
        stringElement.setStart(0L);
        stringElement.setEnd(value.length() - 1);
        put(name, stringElement);
    }

    // @TODO: add description
    public void remove(String name) {
        ArrayList<CLElement> toRemove = new ArrayList<>();
        for (CLElement element : mElements) {
            CLKey key = (CLKey) element;
            if (key.content().equals(name)) {
                toRemove.add(element);
            }
        }
        for (CLElement element : toRemove) {
            mElements.remove(element);
        }
    }

    public void clear() {
        mElements.clear();
    }

    /////////////////////////////////////////////////////////////////////////
    // By name
    /////////////////////////////////////////////////////////////////////////

    // @TODO: add description
    public CLElement get(String name) throws CLParsingException {
        for (CLElement element : mElements) {
            CLKey key = (CLKey) element;
            if (key.content().equals(name)) {
                return key.getValue();
            }
        }
        throw new CLParsingException("no element for key <" + name + ">", this);
    }

    // @TODO: add description
    public int getInt(String name) throws CLParsingException {
        CLElement element = get(name);
        if (element != null) {
            return element.getInt();
        }
        throw new CLParsingException("no int found for key <" + name + ">,"
                + " found [" + element.getStrClass() + "] : " + element, this);
    }

    // @TODO: add description
    public float getFloat(String name) throws CLParsingException {
        CLElement element = get(name);
        if (element != null) {
            return element.getFloat();
        }
        throw new CLParsingException("no float found for key <" + name + ">,"
                + " found [" + element.getStrClass() + "] : " + element, this);
    }

    // @TODO: add description
    public CLArray getArray(String name) throws CLParsingException {
        CLElement element = get(name);
        if (element instanceof CLArray) {
            return (CLArray) element;
        }
        throw new CLParsingException("no array found for key <" + name + ">,"
                + " found [" + element.getStrClass() + "] : " + element, this);
    }

    // @TODO: add description
    public CLObject getObject(String name) throws CLParsingException {
        CLElement element = get(name);
        if (element instanceof CLObject) {
            return (CLObject) element;
        }
        throw new CLParsingException("no object found for key <" + name + ">,"
                + " found [" + element.getStrClass() + "] : " + element, this);
    }

    // @TODO: add description
    public String getString(String name) throws CLParsingException {
        CLElement element = get(name);
        if (element instanceof CLString) {
            return element.content();
        }
        String strClass = null;
        if (element != null) {
            strClass = element.getStrClass();
        }
        throw new CLParsingException("no string found for key <" + name + ">,"
                + " found [" + strClass + "] : " + element, this);
    }

    // @TODO: add description
    public boolean getBoolean(String name) throws CLParsingException {
        CLElement element = get(name);
        if (element instanceof CLToken) {
            return ((CLToken) element).getBoolean();
        }
        throw new CLParsingException("no boolean found for key <" + name + ">,"
                + " found [" + element.getStrClass() + "] : " + element, this);
    }

    /////////////////////////////////////////////////////////////////////////
    // Optional
    /////////////////////////////////////////////////////////////////////////

    // @TODO: add description
    public CLElement getOrNull(String name) {
        for (CLElement element : mElements) {
            CLKey key = (CLKey) element;
            if (key.content().equals(name)) {
                return key.getValue();
            }
        }
        return null;
    }

    // @TODO: add description
    public CLObject getObjectOrNull(String name) {
        CLElement element = getOrNull(name);
        if (element instanceof CLObject) {
            return (CLObject) element;
        }
        return null;
    }

    // @TODO: add description
    public CLArray getArrayOrNull(String name) {
        CLElement element = getOrNull(name);
        if (element instanceof CLArray) {
            return (CLArray) element;
        }
        return null;
    }

    public CLArray getArrayOrCreate(String name) {
        CLArray array = getArrayOrNull(name);
        if (array != null) {
            return array;
        }
        array = new CLArray(new char[]{});
        put(name, array);
        return array;
    }

    // @TODO: add description
    public String getStringOrNull(String name) {
        CLElement element = getOrNull(name);
        if (element instanceof CLString) {
            return element.content();
        }
        return null;
    }

    // @TODO: add description
    public float getFloatOrNaN(String name) {
        CLElement element = getOrNull(name);
        if (element instanceof CLNumber) {
            return element.getFloat();
        }
        return Float.NaN;
    }

    /////////////////////////////////////////////////////////////////////////
    // By index
    /////////////////////////////////////////////////////////////////////////

    // @TODO: add description
    public CLElement get(int index) throws CLParsingException {
        if (index >= 0 && index < mElements.size()) {
            return mElements.get(index);
        }
        throw new CLParsingException("no element at index " + index, this);
    }

    // @TODO: add description
    public int getInt(int index) throws CLParsingException {
        CLElement element = get(index);
        if (element != null) {
            return element.getInt();
        }
        throw new CLParsingException("no int at index " + index, this);
    }

    // @TODO: add description
    public float getFloat(int index) throws CLParsingException {
        CLElement element = get(index);
        if (element != null) {
            return element.getFloat();
        }
        throw new CLParsingException("no float at index " + index, this);
    }

    // @TODO: add description
    public CLArray getArray(int index) throws CLParsingException {
        CLElement element = get(index);
        if (element instanceof CLArray) {
            return (CLArray) element;
        }
        throw new CLParsingException("no array at index " + index, this);
    }

    // @TODO: add description
    public CLObject getObject(int index) throws CLParsingException {
        CLElement element = get(index);
        if (element instanceof CLObject) {
            return (CLObject) element;
        }
        throw new CLParsingException("no object at index " + index, this);
    }

    // @TODO: add description
    public String getString(int index) throws CLParsingException {
        CLElement element = get(index);
        if (element instanceof CLString) {
            return element.content();
        }
        throw new CLParsingException("no string at index " + index, this);
    }

    // @TODO: add description
    public boolean getBoolean(int index) throws CLParsingException {
        CLElement element = get(index);
        if (element instanceof CLToken) {
            return ((CLToken) element).getBoolean();
        }
        throw new CLParsingException("no boolean at index " + index, this);
    }

    /////////////////////////////////////////////////////////////////////////
    // Optional
    /////////////////////////////////////////////////////////////////////////

    // @TODO: add description
    public CLElement getOrNull(int index) {
        if (index >= 0 && index < mElements.size()) {
            return mElements.get(index);
        }
        return null;
    }

    // @TODO: add description
    public String getStringOrNull(int index) {
        CLElement element = getOrNull(index);
        if (element instanceof CLString) {
            return element.content();
        }
        return null;
    }

    @NonNull
    @Override
    public CLContainer clone() {
        CLContainer clone = (CLContainer) super.clone();
        ArrayList<CLElement> clonedArray = new ArrayList<>(mElements.size());
        for (CLElement element: mElements) {
            CLElement elementClone = element.clone();
            elementClone.setContainer(clone);
            clonedArray.add(elementClone);
        }
        clone.mElements = clonedArray;
        return clone;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof CLContainer)) {
            return false;
        }
        return this.mElements.equals(((CLContainer) obj).mElements);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mElements, super.hashCode());
    }
}