001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.mail;
018:
019: import java.util.Random;
020:
021: /**
022: * Utility methods used by commons-email.
023: *
024: * <p>
025: * These methods are copied from other commons components (commons-lang) to avoid creating a dependency for such a small component.
026: * </p>
027: *
028: * <p>
029: * This is a package scoped class, and should not be used directly by users.
030: * </p>
031: *
032: * @author jakarta-commons
033: * @version $Id: EmailUtils.java 279313 2005-09-07 12:41:58Z henning $
034: *
035: * @since 1.0
036: */
037: final class EmailUtils {
038: /**
039: * <p>
040: * Random object used by random method. This has to be not local to the random method so as to not return the same value in the
041: * same millisecond.
042: * </p>
043: */
044: private static final Random RANDOM = new Random();
045:
046: /**
047: * Constructs a new <code>EmailException</code> with no detail message.
048: */
049: private EmailUtils() {
050: super ();
051: }
052:
053: /**
054: * <p>
055: * Checks if a String is empty ("") or null.
056: * </p>
057: *
058: * @param str the String to check, may be null
059: *
060: * @return <code>true</code> if the String is empty or null
061: *
062: * @since Commons Lang v2.1, svn 240418
063: */
064: static boolean isEmpty(String str) {
065: return (str == null) || (str.length() == 0);
066: }
067:
068: /**
069: * <p>
070: * Checks if a String is not empty ("") and not null.
071: * </p>
072: *
073: * @param str the String to check, may be null
074: *
075: * @return <code>true</code> if the String is not empty and not null
076: *
077: * @since Commons Lang v2.1, svn 240418
078: */
079: static boolean isNotEmpty(String str) {
080: return (str != null) && (str.length() > 0);
081: }
082:
083: /**
084: * <p>
085: * Validate an argument, throwing <code>IllegalArgumentException</code> if the argument is <code>null</code>.
086: * </p>
087: *
088: * @param object the object to check is not <code>null</code>
089: * @param message the exception message you would like to see if the object is <code>null</code>
090: *
091: * @throws IllegalArgumentException if the object is <code>null</code>
092: *
093: * @since Commons Lang v2.1, svn 201930
094: */
095: static void notNull(Object object, String message) {
096: if (object == null) {
097: throw new IllegalArgumentException(message);
098: }
099: }
100:
101: /**
102: * <p>
103: * Creates a random string whose length is the number of characters specified.
104: * </p>
105: *
106: * <p>
107: * Characters will be chosen from the set of alphabetic characters.
108: * </p>
109: *
110: * @param count the length of random string to create
111: *
112: * @return the random string
113: *
114: * @since Commons Lang v2.1, svn 201930
115: */
116: static String randomAlphabetic(int count) {
117: return random(count, 0, 0, true, false, null, RANDOM);
118: }
119:
120: /**
121: * <p>
122: * Creates a random string based on a variety of options, using supplied source of randomness.
123: * </p>
124: *
125: * <p>
126: * If start and end are both <code>0</code>, start and end are set to <code>' '</code> and <code>'z'</code>, the ASCII
127: * printable characters, will be used, unless letters and numbers are both <code>false</code>, in which case, start and end
128: * are set to <code>0</code> and <code>Integer.MAX_VALUE</code>.
129: * </p>
130: *
131: * <p>
132: * If set is not <code>null</code>, characters between start and end are chosen.
133: * </p>
134: *
135: * <p>
136: * This method accepts a user-supplied {@link Random} instance to use as a source of randomness. By seeding a single {@link
137: * Random} instance with a fixed seed and using it for each call, the same random sequence of strings can be generated
138: * repeatedly and predictably.
139: * </p>
140: *
141: * @param count the length of random string to create
142: * @param start the position in set of chars to start at
143: * @param end the position in set of chars to end before
144: * @param letters only allow letters?
145: * @param numbers only allow numbers?
146: * @param chars the set of chars to choose randoms from. If <code>null</code>, then it will use the set of all chars.
147: * @param random a source of randomness.
148: *
149: * @return the random string
150: *
151: * @throws IllegalArgumentException if <code>count</code> < 0.
152: *
153: * @since Commons Lang v2.1, svn 201930
154: */
155: private static String random(int count, int start, int end,
156: boolean letters, boolean numbers, char[] chars,
157: Random random) {
158: if (count == 0) {
159: return "";
160: } else if (count < 0) {
161: throw new IllegalArgumentException(
162: "Requested random string length " + count
163: + " is less than 0.");
164: }
165:
166: if ((start == 0) && (end == 0)) {
167: end = 'z' + 1;
168: start = ' ';
169:
170: if (!letters && !numbers) {
171: start = 0;
172: end = Integer.MAX_VALUE;
173: }
174: }
175:
176: StringBuffer buffer = new StringBuffer();
177: int gap = end - start;
178:
179: while (count-- != 0) {
180: char ch;
181:
182: if (chars == null) {
183: ch = (char) (random.nextInt(gap) + start);
184: } else {
185: ch = chars[random.nextInt(gap) + start];
186: }
187:
188: if ((letters && numbers && Character.isLetterOrDigit(ch))
189: || (letters && Character.isLetter(ch))
190: || (numbers && Character.isDigit(ch))
191: || (!letters && !numbers)) {
192: buffer.append(ch);
193: } else {
194: count++;
195: }
196: }
197:
198: return buffer.toString();
199: }
200: }
|