View Javadoc
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.function;
17  
18  import static com.google.common.base.Preconditions.checkArgument;
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import org.apache.commons.lang3.StringUtils;
22  
23  import com.google.common.base.Function;
24  import com.google.common.base.Optional;
25  
26  public final class PrefixFunction implements Function<String, String> {
27  
28  	private final String prefix;
29  	private final Optional<String> separator;
30  
31  	@Override
32  	public String apply(String input) {
33  		checkNotNull(input, "'input' cannot be null");
34  		StringBuilder sb = new StringBuilder();
35  		sb.append(prefix);
36  		if (separator.isPresent()) {
37  			sb.append(separator.get());
38  		}
39  		sb.append(input);
40  		return sb.toString();
41  	}
42  
43  	private PrefixFunction(Builder builder) {
44  		this.prefix = builder.prefix;
45  		this.separator = builder.separator;
46  	}
47  
48  	public static PrefixFunction of(String prefix) {
49  		return builder(prefix).build();
50  	}
51  
52  	public static PrefixFunction of(String prefix, String separator) {
53  		return builder(prefix).separator(separator).build();
54  	}
55  
56  	public static Builder builder(String prefix) {
57  		return new Builder(prefix);
58  	}
59  
60  	public static class Builder implements org.apache.commons.lang3.builder.Builder<PrefixFunction> {
61  
62  		// Required
63  		private final String prefix;
64  
65  		// Optional
66  		private Optional<String> separator = Optional.absent();
67  
68  		public Builder(String prefix) {
69  			this.prefix = prefix;
70  		}
71  
72  		public Builder separator(String separator) {
73  			this.separator = Optional.of(separator);
74  			return this;
75  		}
76  
77  		@Override
78  		public PrefixFunction build() {
79  			PrefixFunction instance = new PrefixFunction(this);
80  			validate(instance);
81  			return instance;
82  		}
83  
84  		private static void validate(PrefixFunction instance) {
85  			checkArgument(!StringUtils.isBlank(instance.prefix), "'prefix' cannot be blank");
86  			checkNotNull(instance.separator, "'separator' cannot be null");
87  			if (instance.separator.isPresent()) {
88  				checkArgument(!StringUtils.isBlank(instance.separator.get()), "'separator' cannot be blank");
89  			}
90  		}
91  
92  		public Optional<String> getSeparator() {
93  			return separator;
94  		}
95  
96  		public void setSeparator(Optional<String> separator) {
97  			this.separator = separator;
98  		}
99  
100 		public String getPrefix() {
101 			return prefix;
102 		}
103 	}
104 
105 }