public class

Helper

extends java.lang.Object

 java.lang.Object

↳androidx.constraintlayout.core.dsl.Helper

Subclasses:

VGuideline, Chain, HChain, Guideline, VChain, Barrier

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
protected java.lang.Stringconfig

protected java.util.Map<java.lang.String, java.lang.String>configMap

protected final java.lang.Stringname

protected static final java.util.Map<Constraint.Side, java.lang.String>sideMap

protected Helper.HelperTypetype

protected static final java.util.Map<Helper.Type, java.lang.String>typeMap

Constructors
publicHelper(java.lang.String name, Helper.HelperType type)

publicHelper(java.lang.String name, Helper.HelperType type, java.lang.String config)

Methods
public voidappend(java.util.Map<java.lang.String, java.lang.String> map, java.lang.StringBuilder ret)

public java.util.Map<java.lang.String, java.lang.String>convertConfigToMap()

public java.lang.StringgetConfig()

public java.lang.StringgetId()

public Helper.HelperTypegetType()

public static voidmain(java.lang.String args[])

public java.lang.StringtoString()

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

Fields

protected final java.lang.String name

protected Helper.HelperType type

protected java.lang.String config

protected java.util.Map<java.lang.String, java.lang.String> configMap

protected static final java.util.Map<Constraint.Side, java.lang.String> sideMap

protected static final java.util.Map<Helper.Type, java.lang.String> typeMap

Constructors

public Helper(java.lang.String name, Helper.HelperType type)

public Helper(java.lang.String name, Helper.HelperType type, java.lang.String config)

Methods

public java.lang.String getId()

public Helper.HelperType getType()

public java.lang.String getConfig()

public java.util.Map<java.lang.String, java.lang.String> convertConfigToMap()

public void append(java.util.Map<java.lang.String, java.lang.String> map, java.lang.StringBuilder ret)

public java.lang.String toString()

public static void main(java.lang.String args[])

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.HashMap;
import java.util.Map;

public class Helper {
    public enum Type {
        VERTICAL_GUIDELINE,
        HORIZONTAL_GUIDELINE,
        VERTICAL_CHAIN,
        HORIZONTAL_CHAIN,
        BARRIER
    }

    protected final String name;
    protected HelperType type = null;
    protected String config;
    protected Map<String, String> configMap = new HashMap<>();
    final static protected Map<Constraint.Side, String> sideMap = new HashMap<>();
    static {
        sideMap.put(Constraint.Side.LEFT, "'left'");
        sideMap.put(Constraint.Side.RIGHT, "'right'");
        sideMap.put(Constraint.Side.TOP, "'top'");
        sideMap.put(Constraint.Side.BOTTOM, "'bottom'");
        sideMap.put(Constraint.Side.START, "'start'");
        sideMap.put(Constraint.Side.END, "'end'");
        sideMap.put(Constraint.Side.BASELINE, "'baseline'");
    }

    final static protected Map<Helper.Type, String> typeMap = new HashMap<>();
    static {
        typeMap.put(Type.VERTICAL_GUIDELINE, "vGuideline");
        typeMap.put(Type.HORIZONTAL_GUIDELINE, "hGuideline");
        typeMap.put(Type.VERTICAL_CHAIN, "vChain");
        typeMap.put(Type.HORIZONTAL_CHAIN, "hChain");
        typeMap.put(Type.BARRIER, "barrier");
    }

    public Helper(String name, HelperType type) {
        this.name = name;
        this.type = type;
    }

    public Helper(String name, HelperType type, String config) {
        this.name = name;
        this.type = type;
        this.config = config;
        configMap = convertConfigToMap();
    }

    public String getId() {
        return name;
    }

    public HelperType getType() {
        return type;
    }

    public String getConfig() {
        return config;
    }

    public Map<String, String> convertConfigToMap() {
        if (config == null || config.length() == 0) {
            return null;
        }

        Map<String, String> map = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        String key = "";
        String value = "";
        int squareBrackets = 0;
        int curlyBrackets = 0;
        char ch;
        for (int i = 0; i < config.length(); i++) {
            ch = config.charAt(i);
            if (ch == ':') {
                key = builder.toString();
                builder.setLength(0);
            } else if (ch == ',' && squareBrackets == 0 && curlyBrackets == 0) {
                value = builder.toString();
                map.put(key, value);
                key = value = "";
                builder.setLength(0);
            } else if (ch != ' ') {
                switch (ch) {
                    case '[':
                        squareBrackets++;
                        break;
                    case '{':
                        curlyBrackets++;
                        break;
                    case ']':
                        squareBrackets--;
                        break;
                    case '}':
                        curlyBrackets--;
                        break;
                }

                builder.append(ch);
            }
        }

        map.put(key, builder.toString());
        return map;
    }

    public void append(Map<String, String> map, StringBuilder ret) {
        if (map.isEmpty()) {
            return;
        }
        for (String key : map.keySet()) {
            ret.append(key).append(":").append(map.get(key)).append(",\n");
        }
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder(name + ":{\n");

        if (type != null) {
            ret.append("type:'").append(type.toString()).append("',\n");
        }

        if (configMap != null) {
            append(configMap, ret);
        }

        ret.append("},\n");
        return ret.toString();
    }

    public static void main(String[] args) {
        Barrier b = new Barrier("abc", "['a1', 'b2']");
        System.out.println(b.toString());
    }

    public static final class HelperType {
        final String mName;

        public HelperType(String str) {
            mName = str;
        }

        @Override
        public String toString() { return mName; }
    }
}