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

brooklyn.util.text.StringShortener Maven / Gradle / Ivy

Go to download

Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.util.text;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/** utility which takes a bunch of segments and applies shortening rules to them */
public class StringShortener {

    protected Map wordsByIdInOrder = new LinkedHashMap();
    protected String separator = null;
    
    protected interface ShorteningRule {
        /** returns the new list, with the relevant items in the list replaced */
        public int apply(LinkedHashMap words, int maxlen, int length);
    }
    
    protected class TruncationRule implements ShorteningRule {
        public TruncationRule(String id, int len) {
            this.id = id;
            this.len = len;
        }
        String id;
        int len;
        
        public int apply(LinkedHashMap words, int maxlen, int length) {
            String v = words.get(id);
            if (v!=null && v.length()>len) {
                int charsToRemove = v.length() - len;
                if (length-charsToRemove < maxlen) charsToRemove = length-maxlen;
                words.put(id, v.substring(0, v.length() - charsToRemove));
                length -= charsToRemove;
                if (charsToRemove==v.length() && separator!=null && length>0)
                    length -= separator.length();
            }
            return length;
        }
    }
    
    protected class RemovalRule implements ShorteningRule {
        public RemovalRule(String id) {
            this.id = id;
        }
        String id;
        
        public int apply(LinkedHashMap words, int maxlen, int length) {
            String v = words.get(id);
            if (v!=null) {
                words.remove(id);
                length -= v.length();
                if (separator!=null && length>0)
                    length -= separator.length();
            }
            return length;
        }
    }
    
    private List rules = new ArrayList();
    

    public StringShortener separator(String separator) {
        this.separator = separator;
        return this;
    }

    public StringShortener append(String id, String text) {
        String old = wordsByIdInOrder.put(id, text);
        if (old!=null) {
            throw new IllegalStateException("Cannot append with id '"+id+"' when id already present");
        }
        // TODO expose a replace or update
        return this;
    }

    public StringShortener truncate(String id, int len) {
        String v = wordsByIdInOrder.get(id);
        if (v!=null && v.length()>len) {
            wordsByIdInOrder.put(id, v.substring(0, len));
        }
        return this;
    }

    public StringShortener canTruncate(String id, int len) {
        rules.add(new TruncationRule(id, len));
        return this;
    }

    public StringShortener canRemove(String id) {
        rules.add(new RemovalRule(id));
        return this;
    }

    public String getStringOfMaxLength(int maxlen) {
        LinkedHashMap words = new LinkedHashMap();
        words.putAll(wordsByIdInOrder);
        int length = 0;
        for (String w: words.values()) {
            if (!Strings.isBlank(w)) {
                length += w.length();
                if (separator!=null)
                    length += separator.length();
            }
        }
        if (separator!=null && length>0)
            // remove trailing separator if one had been added
            length -= separator.length();
        
        List rulesLeft = new ArrayList();
        rulesLeft.addAll(rules);
        
        while (length > maxlen && !rulesLeft.isEmpty()) {
            ShorteningRule r = rulesLeft.remove(0);
            length = r.apply(words, maxlen, length);
        }
        
        StringBuilder sb = new StringBuilder();
        for (String w: words.values()) {
            if (!Strings.isBlank(w)) {
                if (separator!=null && sb.length()>0)
                    sb.append(separator);
                sb.append(w);
            }
        }
        
        String result = sb.toString();
        if (result.length() > maxlen) result = result.substring(0, maxlen);
        
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy