All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softlayer.api.Mask Maven / Gradle / Ivy

There is a newer version: 0.3.4
Show newest version
package com.softlayer.api;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/** Object mask parameter. See http://sldn.softlayer.com/article/Object-Masks */
public class Mask {
    private final Set localProperties = new HashSet();
    private final Map subMasks = new HashMap();
    
    /** Clear out all previously masked objects and local properties */
    public void clear() {
        localProperties.clear();
        subMasks.clear();
    }

    private int getChildCount() {
        return localProperties.size() + subMasks.size();
    }

    protected void withLocalProperty(String localProperty) {
        localProperties.add(localProperty);
    }

    @SuppressWarnings("unchecked")
    protected  T withSubMask(String name, Class maskClass) {
        T subMask = (T) subMasks.get(name);
        if (subMask == null) {
            try {
                subMask = maskClass.newInstance();
            } catch (Exception e) {
                throw new RuntimeException();
            }
            subMasks.put(name, subMask);
        }
        return subMask;
    }

    protected String getMask() {
        return "mask[" + toString() + "]";
    }

    @Override
    public String toString() {
        return toString(new StringBuilder()).toString();
    }

    /** Append this mask's string representation to the given builder and return it */
    public StringBuilder toString(StringBuilder builder) {
        boolean first = true;
        for (String localProperty : localProperties) {
            if (first) {
                first = false;
            } else {
                builder.append(',');
            }
            builder.append(localProperty);
        }

        for (Map.Entry entry : subMasks.entrySet()) {
            if (first) {
                first = false;
            } else {
                builder.append(',');
            }
            builder.append(entry.getKey());

            // No count means add nothing, single is a dot, and multiple means brackets
            int count = entry.getValue().getChildCount();
            if (count == 1) {
                entry.getValue().toString(builder.append('.'));
            } else if (count > 1) {
                entry.getValue().toString(builder.append('[')).append(']');
            }
        }
        return builder;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy