1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.constraintlayout.core.dsl;
18 
19 import java.util.ArrayList;
20 
21 public class Barrier extends Helper {
22     private Constraint.Side mDirection = null;
23     private int mMargin = Integer.MIN_VALUE;
24     private ArrayList<Ref> references = new ArrayList<>();
25 
Barrier(String name)26     public Barrier(String name) {
27         super(name, new HelperType(typeMap.get(Type.BARRIER)));
28     }
29 
Barrier(String name, String config)30     public Barrier(String name, String config) {
31         super(name, new HelperType(typeMap.get(Type.BARRIER)), config);
32         configMap = convertConfigToMap();
33         if (configMap.containsKey("contains")) {
34             Ref.addStringToReferences(configMap.get("contains"), references);
35         }
36     }
37 
38     /**
39      * Get the direction of the Barrier
40      *
41      * @return direction
42      */
getDirection()43     public Constraint.Side getDirection() {
44         return mDirection;
45     }
46 
47     /**
48      * Set the direction of the Barrier
49      *
50      * @param direction
51      */
setDirection(Constraint.Side direction)52     public void setDirection(Constraint.Side direction) {
53         mDirection = direction;
54         configMap.put("direction", sideMap.get(direction));
55     }
56 
57     /**
58      * Get the margin of the Barrier
59      *
60      * @return margin
61      */
getMargin()62     public int getMargin() {
63         return mMargin;
64     }
65 
66     /**
67      * Set the margin of the Barrier
68      *
69      * @param margin
70      */
setMargin(int margin)71     public void setMargin(int margin) {
72         mMargin = margin;
73         configMap.put("margin", String.valueOf(margin));
74     }
75 
76     /**
77      * Convert references into a String representation
78      *
79      * @return a String representation of references
80      */
referencesToString()81     public String referencesToString() {
82         if (references.isEmpty()) {
83             return "";
84         }
85 
86         StringBuilder builder = new StringBuilder("[");
87         for (Ref ref : references) {
88             builder.append(ref.toString());
89         }
90         builder.append("]");
91         return builder.toString();
92     }
93 
94     /**
95      * Add a new reference
96      *
97      * @param ref reference
98      * @return Barrier
99      */
addReference(Ref ref)100     public Barrier addReference(Ref ref) {
101         references.add(ref);
102         configMap.put("contains", referencesToString());
103         return this;
104     }
105 
106     /**
107      * Add a new reference
108      *
109      * @param ref reference in a String representation
110      * @return Chain
111      */
addReference(String ref)112     public Barrier addReference(String ref) {
113         return addReference(Ref.parseStringToRef(ref));
114     }
115 }