public class

CLElement

extends java.lang.Object

implements java.lang.Cloneable

 java.lang.Object

↳androidx.constraintlayout.core.parser.CLElement

Subclasses:

CLString, CLContainer, CLKey, CLArray, CLToken, CLNumber, 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/)

Overview

Base element to represent a piece of parsed Json.

Summary

Fields
protected CLContainermContainer

protected longmEnd

protected longmStart

protected static intsBaseIndent

protected static intsMaxLine

Constructors
publicCLElement(char[] content[])

Methods
protected voidaddIndent(java.lang.StringBuilder builder, int indent)

public CLElementclone()

public java.lang.Stringcontent()

public booleanequals(java.lang.Object o)

public CLElementgetContainer()

protected java.lang.StringgetDebugName()

public longgetEnd()

The character index this element was ended on

public floatgetFloat()

public intgetInt()

public intgetLine()

get the line Number

public longgetStart()

The character index this element was started on

protected java.lang.StringgetStrClass()

public booleanhasContent()

Whether this element has any valid content defined.

public inthashCode()

public booleanisDone()

public booleanisStarted()

public booleannotStarted()

public voidsetContainer(CLContainer element)

public voidsetEnd(long end)

public voidsetLine(int line)

public voidsetStart(long start)

protected java.lang.StringtoFormattedJSON(int indent, int forceIndent)

protected java.lang.StringtoJSON()

public java.lang.StringtoString()

from java.lang.Objectfinalize, getClass, notify, notifyAll, wait, wait, wait

Fields

protected long mStart

protected long mEnd

protected CLContainer mContainer

protected static int sMaxLine

protected static int sBaseIndent

Constructors

public CLElement(char[] content[])

Methods

public boolean notStarted()

public void setLine(int line)

public int getLine()

get the line Number

Returns:

return the line number this element was on

public void setStart(long start)

public long getStart()

The character index this element was started on

public long getEnd()

The character index this element was ended on

public void setEnd(long end)

protected void addIndent(java.lang.StringBuilder builder, int indent)

public java.lang.String toString()

protected java.lang.String getStrClass()

protected java.lang.String getDebugName()

public java.lang.String content()

public boolean hasContent()

Whether this element has any valid content defined.

The content is valid when CLElement.content() can be called without causing exceptions.

public boolean isDone()

public void setContainer(CLContainer element)

public CLElement getContainer()

public boolean isStarted()

protected java.lang.String toJSON()

protected java.lang.String toFormattedJSON(int indent, int forceIndent)

public int getInt()

public float getFloat()

public boolean equals(java.lang.Object o)

public int hashCode()

public CLElement clone()

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.Arrays;
import java.util.Objects;

/**
 * Base element to represent a piece of parsed Json.
 */
public class CLElement implements Cloneable {

    private final char[] mContent;
    protected long mStart = -1;
    protected long mEnd = Long.MAX_VALUE;
    protected CLContainer mContainer;
    private int mLine;

    protected static int sMaxLine = 80; // Max number of characters before the formatter indents
    protected static int sBaseIndent = 2; // default indentation value

    public CLElement(char[] content) {
        mContent = content;
    }

    // @TODO: add description
    public boolean notStarted() {
        return mStart == -1;
    }

    public void setLine(int line) {
        this.mLine = line;
    }

    /**
     * get the line Number
     *
     * @return return the line number this element was on
     */
    public int getLine() {
        return mLine;
    }

    public void setStart(long start) {
        this.mStart = start;
    }

    /**
     * The character index this element was started on
     */
    public long getStart() {
        return this.mStart;
    }

    /**
     * The character index this element was ended on
     */
    public long getEnd() {
        return this.mEnd;
    }

    // @TODO: add description
    public void setEnd(long end) {
        if (this.mEnd != Long.MAX_VALUE) {
            return;
        }
        this.mEnd = end;
        if (CLParser.sDebug) {
            System.out.println("closing " + this.hashCode() + " -> " + this);
        }
        if (mContainer != null) {
            mContainer.add(this);
        }
    }

    protected void addIndent(StringBuilder builder, int indent) {
        for (int i = 0; i < indent; i++) {
            builder.append(' ');
        }
    }

    @Override
    public String toString() {
        if (mStart > mEnd || mEnd == Long.MAX_VALUE) {
            return this.getClass() + " (INVALID, " + mStart + "-" + mEnd + ")";
        }
        String content = new String(mContent);
        content = content.substring((int) mStart, (int) mEnd + 1);

        return getStrClass() + " (" + mStart + " : " + mEnd + ") <<" + content + ">>";
    }

    protected String getStrClass() {
        String myClass = this.getClass().toString();
        return myClass.substring(myClass.lastIndexOf('.') + 1);
    }

    protected String getDebugName() {
        if (CLParser.sDebug) {
            return getStrClass() + " -> ";
        }
        return "";
    }

    // @TODO: add description
    public String content() {
        String content = new String(mContent);
        // Handle empty string
        if (content.length() < 1) {
            return "";
        }
        if (mEnd == Long.MAX_VALUE || mEnd < mStart) {
            return content.substring((int) mStart, (int) mStart + 1);
        }
        return content.substring((int) mStart, (int) mEnd + 1);
    }

    /**
     * Whether this element has any valid content defined.
     * <p>
     * The content is valid when {@link #content()} can be called without causing exceptions.
     */
    public boolean hasContent() {
        return mContent != null && mContent.length >= 1;
    }

    public boolean isDone() {
        return mEnd != Long.MAX_VALUE;
    }

    public void setContainer(CLContainer element) {
        mContainer = element;
    }

    public CLElement getContainer() {
        return mContainer;
    }

    public boolean isStarted() {
        return mStart > -1;
    }

    protected String toJSON() {
        return "";
    }

    protected String toFormattedJSON(int indent, int forceIndent) {
        return "";
    }

    // @TODO: add description
    public int getInt() {
        if (this instanceof CLNumber) {
            return ((CLNumber) this).getInt();
        }
        return 0;
    }

    // @TODO: add description
    public float getFloat() {
        if (this instanceof CLNumber) {
            return ((CLNumber) this).getFloat();
        }
        return Float.NaN;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CLElement)) return false;

        CLElement clElement = (CLElement) o;

        if (mStart != clElement.mStart) return false;
        if (mEnd != clElement.mEnd) return false;
        if (mLine != clElement.mLine) return false;
        if (!Arrays.equals(mContent, clElement.mContent)) return false;
        return Objects.equals(mContainer, clElement.mContainer);
    }

    @Override
    public int hashCode() {
        // Auto-generated with Intellij Action "equals() and hashcode()"
        int result = Arrays.hashCode(mContent);
        result = 31 * result + (int) (mStart ^ (mStart >>> 32));
        result = 31 * result + (int) (mEnd ^ (mEnd >>> 32));
        result = 31 * result + (mContainer != null ? mContainer.hashCode() : 0);
        result = 31 * result + mLine;
        return result;
    }

    @NonNull
    @Override
    public CLElement clone() {
        try {
            return (CLElement) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}