public class

Ref

extends java.lang.Object

 java.lang.Object

↳androidx.constraintlayout.core.dsl.Ref

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

Methods
public static voidaddStringToReferences(java.lang.String str, java.util.ArrayList<Ref> refs)

Add references in a String representation to a Ref ArrayList Used to add the Ref(s) property in the Config to references

public java.lang.StringgetId()

Get the Id of the reference

public floatgetPostMargin()

Get the postMargin of the reference

public floatgetPreMargin()

Get the preMargin of the reference

public floatgetWeight()

Get the weight of the reference

public static floatparseFloat(java.lang.Object obj)

Try to parse an object into a float number

public static RefparseStringToRef(java.lang.String str)

public voidsetId(java.lang.String id)

Set the Id of the reference

public voidsetPostMargin(float postMargin)

Set the postMargin of the reference

public voidsetPreMargin(float preMargin)

Set the preMargin of the reference

public voidsetWeight(float weight)

Set the weight of the reference

public java.lang.StringtoString()

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Methods

public java.lang.String getId()

Get the Id of the reference

Returns:

the Id of the reference

public void setId(java.lang.String id)

Set the Id of the reference

Parameters:

id:

public float getWeight()

Get the weight of the reference

Returns:

the weight of the reference

public void setWeight(float weight)

Set the weight of the reference

Parameters:

weight:

public float getPreMargin()

Get the preMargin of the reference

Returns:

the preMargin of the reference

public void setPreMargin(float preMargin)

Set the preMargin of the reference

Parameters:

preMargin:

public float getPostMargin()

Get the postMargin of the reference

Returns:

the preMargin of the reference

public void setPostMargin(float postMargin)

Set the postMargin of the reference

Parameters:

postMargin:

public static float parseFloat(java.lang.Object obj)

Try to parse an object into a float number

Parameters:

obj: object to be parsed

Returns:

a number

public static Ref parseStringToRef(java.lang.String str)

public static void addStringToReferences(java.lang.String str, java.util.ArrayList<Ref> refs)

Add references in a String representation to a Ref ArrayList Used to add the Ref(s) property in the Config to references

Parameters:

str: references in a String representation
refs: a Ref ArrayList

public java.lang.String toString()

Source

/*
 * Copyright (C) 2022 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.dsl;

import java.util.ArrayList;
import java.util.Arrays;

public class Ref {
    private String mId;
    private float mWeight = Float.NaN;
    private float mPreMargin = Float.NaN;
    private float mPostMargin = Float.NaN;

    Ref(String id) {
        mId = id;
    }

    Ref(String id, float weight) {
        mId = id;
        mWeight = weight;
    }

    Ref(String id, float weight, float preMargin) {
        mId = id;
        mWeight = weight;
        mPreMargin = preMargin;
    }

    Ref(String id, float weight, float preMargin, float postMargin) {
        mId = id;
        mWeight = weight;
        mPreMargin = preMargin;
        mPostMargin = postMargin;
    }

    /**
     * Get the Id of the reference
     *
     * @return the Id of the reference
     */
    public String getId() {
        return mId;
    }

    /**
     * Set the Id of the reference
     *
     * @param id
     */
    public void setId(String id) {
        mId = id;
    }

    /**
     * Get the weight of the reference
     *
     * @return the weight of the reference
     */
    public float getWeight() {
        return mWeight;
    }

    /**
     * Set the weight of the reference
     *
     * @param weight
     */
    public void setWeight(float weight) {
        mWeight = weight;
    }

    /**
     * Get the preMargin of the reference
     *
     * @return the preMargin of the reference
     */
    public float getPreMargin() {
        return mPreMargin;
    }

    /**
     * Set the preMargin of the reference
     *
     * @param preMargin
     */
    public void setPreMargin(float preMargin) {
        mPreMargin = preMargin;
    }

    /**
     * Get the postMargin of the reference
     *
     * @return the preMargin of the reference
     */
    public float getPostMargin() {
        return mPostMargin;
    }

    /**
     * Set the postMargin of the reference
     *
     * @param postMargin
     */
    public void setPostMargin(float postMargin) {
        mPostMargin = postMargin;
    }

    /**
     * Try to parse an object into a float number
     *
     * @param obj object to be parsed
     * @return a number
     */
    static public float parseFloat(Object obj) {
        float val = Float.NaN;
        try {
            val = Float.parseFloat(obj.toString());
        } catch (Exception e) {
            // ignore
        }
        return val;
    }

    static public Ref parseStringToRef(String str) {
        String[] values = str.replaceAll("[\\[\\]\\']", "").split(",");
        if (values.length == 0) {
            return null;
        }
        Object[] arr = new Object[4];
        for (int i = 0; i < values.length; i++) {
            if (i >= 4) {
                break;
            }
            arr[i] = values[i];
        }
        return new Ref(arr[0].toString().replace("'", ""), parseFloat(arr[1]),
                parseFloat(arr[2]), parseFloat(arr[3]));
    }

    /**
     * Add references in a String representation to a Ref ArrayList
     * Used to add the Ref(s) property in the Config to references
     *
     * @param str references in a String representation
     * @param refs  a Ref ArrayList
     */
    static public void addStringToReferences(String str, ArrayList<Ref> refs) {
        if (str == null || str.length() == 0) {
            return;
        }

        Object[] arr = new Object[4];
        StringBuilder builder = new StringBuilder();
        int squareBrackets = 0;
        int varCount = 0;
        char ch;

        for (int i = 0; i < str.length(); i++) {
            ch = str.charAt(i);
            switch (ch) {
                case '[':
                    squareBrackets++;
                    break;
                case ']':
                    if (squareBrackets > 0) {
                        squareBrackets--;
                        arr[varCount] = builder.toString();
                        builder.setLength(0);
                        if (arr[0] != null) {
                            refs.add(new Ref(arr[0].toString(), parseFloat(arr[1]),
                                    parseFloat(arr[2]), parseFloat(arr[3])));
                            varCount = 0;
                            Arrays.fill(arr, null);
                        }
                    }
                    break;
                case ',':
                    // deal with the first 3 values in the nested array,
                    // the fourth value (postMargin) would be handled at case ']'
                    if (varCount < 3) {
                        arr[varCount++] = builder.toString();
                        builder.setLength(0);
                    }
                    // squareBrackets == 1 indicate the value is not in a nested array.
                    if (squareBrackets == 1 && arr[0] != null) {
                        refs.add(new Ref(arr[0].toString()));
                        varCount = 0;
                        arr[0] = null;
                    }
                    break;
                case ' ':
                case '\'':
                    break;
                default:
                    builder.append(ch);
            }
        }
    }

    @Override
    public String toString() {
        if (mId == null || mId.length() == 0) {
            return "";
        }

        StringBuilder ret = new StringBuilder();
        boolean isArray = false;
        if (!Float.isNaN(mWeight) || !Float.isNaN(mPreMargin)
                || !Float.isNaN(mPostMargin)) {
            isArray = true;
        }
        if (isArray) {
            ret.append("[");
        }
        ret.append("'").append(mId).append("'");

        if (!Float.isNaN(mPostMargin)) {
            ret.append(",").append(!Float.isNaN(mWeight) ? mWeight : 0).append(",");
            ret.append(!Float.isNaN(mPreMargin) ? mPreMargin : 0).append(",");
            ret.append(mPostMargin);
        } else if (!Float.isNaN(mPreMargin)) {
            ret.append(",").append(!Float.isNaN(mWeight) ? mWeight : 0).append(",");
            ret.append(mPreMargin);
        } else if (!Float.isNaN(mWeight)) {
            ret.append(",").append(mWeight);
        }

        if(isArray) {
            ret.append("]");
        }
        ret.append(",");
        return ret.toString();
    }
}