1 /** 2 * Copyright 2010-2014 The Kuali Foundation 3 * 4 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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 package org.kuali.common.util.base.string; 17 18 import static com.google.common.base.Preconditions.checkArgument; 19 import static com.google.common.collect.Sets.newHashSet; 20 import static org.kuali.common.util.base.Precondition.checkNotBlank; 21 import static org.kuali.common.util.base.Precondition.checkNotNull; 22 23 import java.util.List; 24 import java.util.Set; 25 26 import com.google.common.base.Function; 27 import com.google.common.base.Splitter; 28 import com.google.common.collect.ImmutableSet; 29 30 /** 31 * <p> 32 * Convert a set of delimited strings into an immutable set of individual elements. 33 * </p> 34 * 35 * A set containing these two strings: 36 * 37 * <pre> 38 * java.class.path 39 * java.class.version 40 * </pre> 41 * 42 * Converts to a set containing these four strings: 43 * 44 * <pre> 45 * java 46 * java.class 47 * java.class.path 48 * java.class.version 49 * </pre> 50 */ 51 public final class SplitterFunction implements Function<Set<String>, Set<String>> { 52 53 public SplitterFunction() { 54 this("."); 55 } 56 57 public SplitterFunction(String separator) { 58 this.separator = checkNotNull(separator, "separator"); 59 this.splitter = Splitter.on(separator); 60 } 61 62 private final Splitter splitter; 63 private final String separator; 64 65 /** 66 * <p> 67 * Convert a set of delimited strings into an immutable set of individual elements. 68 * </p> 69 * 70 * A set containing these two strings: 71 * 72 * <pre> 73 * java.class.path 74 * java.class.version 75 * </pre> 76 * 77 * Converts to a set containing these four strings: 78 * 79 * <pre> 80 * java 81 * java.class 82 * java.class.path 83 * java.class.version 84 * </pre> 85 * 86 * @throws IllegalArgumentException 87 * If splitting the individual strings into elements produces blank tokens 88 * @throws NullPointerException 89 * If strings is null or contains null elements 90 */ 91 @Override 92 public Set<String> apply(Set<String> strings) { 93 checkNotNull(strings, "strings"); 94 Set<String> set = newHashSet(); 95 for (String string : strings) { 96 set.addAll(apply(string)); 97 } 98 return ImmutableSet.copyOf(set); 99 } 100 101 /** 102 * Convert a delimited string into a set of individual elements. 103 * 104 * <pre> 105 * java.class.path -> java 106 * java.class 107 * java.class.path 108 * </pre> 109 * 110 * @throws IllegalArgumentException 111 * If splitting the string into elements produces blank tokens 112 */ 113 protected Set<String> apply(String string) { 114 checkNotNull(string, "string"); 115 116 // Split the key into tokens 117 List<String> tokens = splitter.splitToList(string); 118 119 // Setup some storage for the strings we are creating 120 Set<String> strings = newHashSet(); 121 122 // Allocate a string builder 123 StringBuilder sb = new StringBuilder(); 124 125 // Iterate over the tokens to create unique string elements 126 for (int i = 0; i < tokens.size(); i++) { 127 128 // append the separator unless this is the first loop iteration 129 sb = (i != 0) ? sb.append(separator) : sb; 130 131 // Extract the token, checking to make sure it isn't blank 132 String token = checkNotBlank(tokens.get(i), "token"); 133 134 // Append the current token to create a new element 135 String element = sb.append(token).toString(); 136 137 // Make sure it was actually added 138 // TODO Don't think checkArgument() here is necessary since checkNotBlank() prevents token from being the empty string 139 checkArgument(strings.add(element), "%s is a duplicate token -> [%s]", element, string); 140 } 141 142 // Return what we've got 143 return strings; 144 } 145 146 public String getSeparator() { 147 return separator; 148 } 149 150 public Splitter getSplitter() { 151 return splitter; 152 } 153 154 }