();
for (T element : c) {
if (element != null) {
nonNull.add(element);
}
}
return nonNull;
}
private Collections() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/Dates.java 0000664 0000000 0000000 00000020710 12430175307 0022501 0 ustar 00root root 0000000 0000000 /*
* Created on Jan 22, 2011
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* Copyright @2011 the original author or authors.
*/
package org.fest.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Utility methods related to dates.
*
* @author Joel Costigliola
*/
public class Dates {
/**
* ISO 8601 date format (yyyy-MM-dd), example : 2003-04-23
*/
public static final DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
/**
* ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss), example : 2003-04-26T13:01:02
*/
public static final DateFormat ISO_DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
/**
* ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss.SSS), example : 2003-04-26T03:01:02.999
*/
public static final DateFormat ISO_DATE_TIME_FORMAT_WITH_MS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
/**
* Formats the given date using the ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss).
* Method in synchronized because SimpleDateFormat is not thread safe (sigh).
*
* Returns null if given the date is null.
* @param date the date to format.
* @return the formatted date or null if given the date was null.
*/
public static synchronized String formatAsDatetime(Date date) {
return date == null ? null : ISO_DATE_TIME_FORMAT.format(date);
}
/**
* Formats the given date using the ISO 8601 date-time format with millisecond (yyyy-MM-dd'T'HH:mm:ss:SSS).
* Method in synchronized because SimpleDateFormat is not thread safe (sigh).
*
* Returns null if given the date is null.
* @param date the date to format.
* @return the formatted date or null if given the date was null.
*/
public static synchronized String formatAsDatetimeWithMs(Date date) {
return date == null ? null : ISO_DATE_TIME_FORMAT_WITH_MS.format(date);
}
/**
* Formats the date of the given calendar using the ISO 8601 date-time format (yyyy-MM-dd'T'HH:mm:ss).
* Method is thread safe.
*
* Returns null if the given calendar is null.
* @param calendar the calendar to format.
* @return the formatted calendar or null if the given calendar was null.
*/
public static String formatAsDatetime(Calendar calendar) {
return calendar == null ? null : formatAsDatetime(calendar.getTime());
}
/**
* Utility method to parse a Date following {@link #ISO_DATE_FORMAT}, returns null if the given String is null.
* @param dateAsString the string to parse as a Date following {@link #ISO_DATE_FORMAT}
* @return the corrresponding Date or null if the given String is null.
* @throws RuntimeException encapsulating ParseException if the string can't be parsed as a Date
*/
public static Date parse(String dateAsString) {
try {
return dateAsString == null ? null : ISO_DATE_FORMAT.parse(dateAsString);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* Utility method to parse a Date following {@link #ISO_DATE_TIME_FORMAT}, returns null if the given String is null.
* @param dateAsString the string to parse as a Date following {@link #ISO_DATE_TIME_FORMAT}
* @return the corrresponding Date with time details or null if the given String is null.
* @throws RuntimeException encapsulating ParseException if the string can't be parsed as a Date
*/
public static Date parseDatetime(String dateAsString) {
try {
return dateAsString == null ? null : ISO_DATE_TIME_FORMAT.parse(dateAsString);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* Converts the given Date to Calendar, returns null if the given Date is null.
* @param date the date to convert to a Calendar.
* @return the Calendar corresponding to the given Date or null if the given Date is null.
*/
public static Calendar toCalendar(Date date) {
if (date == null) { return null; }
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
/**
* Extracts the year of the given Date.
* @param date the date to extract the year from - must not be null.
* @return the year of the given Date
* @throws NullPointerException if given Date is null
*/
public static int yearOf(Date date) {
return toCalendar(date).get(Calendar.YEAR);
}
/**
* Dates Extracts the month of the given Date starting at 1 (January=1, February=2, ...).
* @param date the date to extract the month from - must not be null.
* @return the month of the given Date starting at 1 (January=1, February=2, ...)
* @throws NullPointerException if given Date is null
*/
public static int monthOf(Date date) {
return toCalendar(date).get(Calendar.MONTH) + 1; // based 1 month (January=1)
}
/**
* Dates Extracts the day of month of the given Date.
* @param date the date to extract the day of month from - must not be null.
* @return the day of month of the given Date
* @throws NullPointerException if given Date is null
*/
public static int dayOfMonthOf(Date date) {
return toCalendar(date).get(Calendar.DAY_OF_MONTH);
}
/**
* Extracts the day of week of the given Date, returned value follows {@link Calendar#DAY_OF_WEEK} .
* @param date the date to extract the day of week from - must not be null.
* @return the day of week of the given Date
* @throws NullPointerException if given Date is null
*/
public static int dayOfWeekOf(Date date) {
return toCalendar(date).get(Calendar.DAY_OF_WEEK);
}
/**
* Extracts the hour of day if the given Date (24-hour clock).
* @param date the date to extract the hour of day from - must not be null.
* @return the hour of day of the given Date (24-hour clock)
* @throws NullPointerException if given Date is null
*/
public static int hourOfDay(Date date) {
return toCalendar(date).get(Calendar.HOUR_OF_DAY);
}
/**
* Dates Extracts the minute of the given Date.
* @param date the date to extract the minute from - must not be null.
* @return the minute of the given Date
* @throws NullPointerException if given Date is null
*/
public static int minuteOf(Date date) {
return toCalendar(date).get(Calendar.MINUTE);
}
/**
* Extracts the second of the given Date.
* @param date the date to extract the second from - must not be null.
* @return the second of the given Date
* @throws NullPointerException if given Date is null
*/
public static int secondOf(Date date) {
return toCalendar(date).get(Calendar.SECOND);
}
/**
* Extracts the millisecond of the given Date.
* @param date the date to extract the millisecond from - must not be null.
* @return the millisecond of the given Date
* @throws NullPointerException if given Date is null
*/
public static int millisecondOf(Date date) {
return toCalendar(date).get(Calendar.MILLISECOND);
}
/**
* Returns a copy of the given date without the time part (which is set to 00:00:00), for example :
* truncateTime(2008-12-29T23:45:12)
will give 2008-12-29T00:00:00
.
*
* Returns null if the given Date is null.
* @param date we want to get the day part (the parameter is read only).
* @return the truncated date.
*/
public static Date truncateTime(Date date) {
if (date == null) { return null; }
Calendar cal = toCalendar(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date today() {
return new Date();
}
public static Date yesterday() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
public static Date tomorrow() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
}
fest-util-1.2.5/src/main/java/org/fest/util/FailureMessages.java 0000664 0000000 0000000 00000001665 12430175307 0024530 0 ustar 00root root 0000000 0000000 /*
* Created on Sep 7, 2010
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* Copyright @2010-2011 the original author or authors.
*/
package org.fest.util;
/**
* @author Alex Ruiz
*/
public final class FailureMessages {
public static String actualIsEmpty() {
return "expecting actual not to be empty";
}
public static String actualIsNull() {
return "expecting actual not to be null";
}
private FailureMessages() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/Files.java 0000664 0000000 0000000 00000024326 12430175307 0022512 0 ustar 00root root 0000000 0000000 /*
* Created on Sep 23, 2006
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2006-2012 the original author or authors.
*/
package org.fest.util;
import static java.io.File.separator;
import static java.lang.String.*;
import static org.fest.util.Arrays.isNullOrEmpty;
import static org.fest.util.Closeables.closeQuietly;
import static org.fest.util.Flushables.flush;
import static org.fest.util.Strings.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* Utility methods related to files.
*
* @author Yvonne Wang
* @author Alex Ruiz
*/
public class Files {
/**
* Returns the names of the files inside the specified directory.
*
* @param dirName the name of the directory to start the search from.
* @param recurse if {@code true}, we will look in subdirectories.
* @return the names of the files inside the specified directory.
* @throws IllegalArgumentException if the given directory name does not point to an existing directory.
*/
public static List fileNamesIn(String dirName, boolean recurse) {
File dir = new File(dirName);
if (!dir.isDirectory()) {
throw new IllegalArgumentException(format("%s is not a directory", quote(dirName)));
}
return fileNamesIn(dir, recurse);
}
/**
* Returns the names of the files inside the specified directory.
*
* @param dir the name of the directory to start the search from.
* @param recurse if {@code true}, we will look in subdirectories.
* @return the names of the files inside the specified directory.
*/
private static List fileNamesIn(File dir, boolean recurse) {
List scriptNames = new ArrayList();
File[] existingFiles = dir.listFiles();
if (isNullOrEmpty(existingFiles)) {
return scriptNames;
}
for (File existingFile : existingFiles) {
if (existingFile.isDirectory()) {
if (recurse) {
scriptNames.addAll(fileNamesIn(existingFile, recurse));
}
continue;
}
String filename = existingFile.getAbsolutePath();
if (!scriptNames.contains(filename)) {
scriptNames.add(filename);
}
}
return scriptNames;
}
/**
* Returns the system's temporary directory.
*
* @return the system's temporary directory.
* @throws FilesException if this method cannot find or create the system's temporary directory.
*/
public static File temporaryFolder() {
File temp = new File(temporaryFolderPath());
if (!temp.isDirectory()) {
throw new FilesException("Unable to find temporary directory");
}
return temp;
}
/**
* Returns the path of the system's temporary directory. This method appends the system's file separator at the end of
* the path.
*
* @return the path of the system's temporary directory.
*/
public static String temporaryFolderPath() {
return append(separator).to(System.getProperty("java.io.tmpdir"));
}
/**
* Creates a new file in the system's temporary directory. The name of the file will be the result of:
*
*
* concat(String.valueOf(System.currentTimeMillis()), ".txt");
*
*
* @return the created file.
*/
public static File newTemporaryFile() {
String tempFileName = concat(valueOf(System.currentTimeMillis()), ".txt");
return newFile(concat(temporaryFolderPath(), tempFileName));
}
/**
* Creates a new directory in the system's temporary directory. The name of the directory will be the result of:
*
*
* System.currentTimeMillis();
*
*
* @return the created file.
*/
public static File newTemporaryFolder() {
String tempFileName = String.valueOf(System.currentTimeMillis());
return newFolder(concat(temporaryFolderPath(), tempFileName));
}
/**
* Creates a new file using the given path.
*
* @param path the path of the new file.
* @return the new created file.
* @throws FilesException if the path belongs to an existing non-empty directory.
* @throws FilesException if the path belongs to an existing file.
* @throws FilesException if any I/O error is thrown when creating the new file.
*/
public static File newFile(String path) {
File file = new File(path);
if (file.isDirectory() && !isNullOrEmpty(file.list())) {
throw cannotCreateNewFile(path, "a non-empty directory was found with the same path");
}
try {
if (!file.createNewFile()) {
throw cannotCreateNewFile(path, "a file was found with the same path");
}
} catch (IOException e) {
throw cannotCreateNewFile(path, e);
}
return file;
}
/**
* Creates a new directory using the given path.
*
* @param path the path of the new directory.
* @return the new created directory.
* @throws FilesException if the path belongs to an existing non-empty directory.
* @throws FilesException if the path belongs to an existing file.
* @throws FilesException if any I/O error is thrown when creating the new directory.
*/
public static File newFolder(String path) {
File file = new File(path);
if (file.isDirectory() && !isNullOrEmpty(file.list())) {
throw cannotCreateNewFile(path, "a non-empty directory was found with the same path");
}
try {
if (!file.mkdir()) {
throw cannotCreateNewFile(path, "a file was found with the same path");
}
} catch (Exception e) {
throw cannotCreateNewFile(path, e);
}
return file;
}
private static FilesException cannotCreateNewFile(String path, String reason) {
throw cannotCreateNewFile(path, reason, null);
}
private static FilesException cannotCreateNewFile(String path, Exception cause) {
throw cannotCreateNewFile(path, null, cause);
}
private static FilesException cannotCreateNewFile(String path, String reason, Exception cause) {
String message = String.format("Unable to create the new file %s", quote(path));
if (!Strings.isNullOrEmpty(reason)) {
message = concat(message, ": ", reason);
}
if (cause != null) {
throw new FilesException(message, cause);
}
throw new FilesException(message);
}
/**
* Flushes and closes the given {@link Writer}
. Any I/O errors catched by this method are ignored and not
* rethrown.
*
* @param writer the writer to flush and close.
*/
public static void flushAndClose(Writer writer) {
if (writer == null) {
return;
}
flush(writer);
closeQuietly(writer);
}
/**
* Flushes and closes the given {@link OutputStream}
. Any I/O errors catched by this method are ignored
* and not rethrown.
*
* @param out the output stream to flush and close.
*/
public static void flushAndClose(OutputStream out) {
if (out == null) {
return;
}
flush(out);
closeQuietly(out);
}
/**
* Returns the current directory.
*
* @return the current directory.
* @throws FilesException if the current directory cannot be obtained.
*/
public static File currentFolder() {
try {
return new File(".").getCanonicalFile();
} catch (IOException e) {
throw new FilesException("Unable to get current directory", e);
}
}
/**
* Deletes the given file or directory.
*
* @param file the file or directory to delete.
*/
public static void delete(File file) {
if (file.isFile()) {
file.delete();
return;
}
if (!file.isDirectory()) {
return;
}
for (File f : file.listFiles()) {
delete(f);
}
file.delete();
}
/**
* Loads the text content of a file into a character string.
*
* @param file the file.
* @param charsetName the name of the character set to use.
* @return the content of the file.
* @throws IllegalArgumentException if the given character set is not supported on this platform.
* @throws FilesException if an I/O exception occurs.
*/
public static String contentOf(File file, String charsetName) {
if (!Charset.isSupported(charsetName)) {
throw new IllegalArgumentException(String.format("Charset:<'%s'> is not supported on this system", charsetName));
}
return contentOf(file, Charset.forName(charsetName));
}
/**
* Loads the text content of a file into a character string.
*
* @param file the file.
* @param charset the character set to use.
* @return the content of the file.
* @throws NullPointerException if the given charset is {@code null}.
* @throws FilesException if an I/O exception occurs.
*/
public static String contentOf(File file, Charset charset) {
if (charset == null) {
throw new NullPointerException("The charset should not be null");
}
try {
return loadContents(file, charset);
} catch (IOException e) {
throw new FilesException("Unable to read " + file.getAbsolutePath(), e);
}
}
private static String loadContents(File file, Charset charset) throws IOException {
BufferedReader reader = null;
boolean threw = true;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
StringWriter writer = new StringWriter();
int c;
while ((c = reader.read()) != -1) {
writer.write(c);
}
threw = false;
return writer.toString();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
if (!threw)
{
throw e; // if there was an initial exception, don't hide it
}
}
}
}
}
private Files() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/FilesException.java 0000664 0000000 0000000 00000002410 12430175307 0024357 0 ustar 00root root 0000000 0000000 /*
* Created on May 16, 2007
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2007-2012 the original author or authors.
*/
package org.fest.util;
/**
* Exception thrown by {@link Files}
.
*
* @author Yvonne Wang
*/
public final class FilesException extends RuntimeException {
private static final long serialVersionUID = -8328554403430790831L;
/**
* Creates a new {@link FilesException}.
*
* @param message the detail message.
*/
public FilesException(String message) {
super(message);
}
/**
* Creates a new {@link FilesException}.
*
* @param message the detail message.
* @param cause the cause of the error.
*/
public FilesException(String message, Throwable cause) {
super(message, cause);
}
}
fest-util-1.2.5/src/main/java/org/fest/util/Flushables.java 0000664 0000000 0000000 00000002417 12430175307 0023535 0 ustar 00root root 0000000 0000000 /*
* Created on Jan 15, 2008
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2008-2012 the original author or authors.
*/
package org.fest.util;
import java.io.Flushable;
/**
* Utility methods related to {@link Flushable}
.
*
* @author Yvonne Wang
*/
public class Flushables {
/**
* Flushes the given {@link Flushable}
s, ignoring any thrown exceptions.
*
* @param flushables the {@code Flushable}s to flush.
*/
public static void flush(Flushable... flushables) {
for (Flushable f : flushables) {
flushFlushable(f);
}
}
private static void flushFlushable(Flushable f) {
if (f == null) {
return;
}
try {
f.flush();
} catch (Exception e) {
}
}
private Flushables() {
}
}
fest-util-1.2.5/src/main/java/org/fest/util/Introspection.java 0000664 0000000 0000000 00000007311 12430175307 0024303 0 ustar 00root root 0000000 0000000 /*
* Created on Jun 28, 2010
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2010-2012 the original author or authors.
*/
package org.fest.util;
import static java.lang.String.format;
import static java.lang.reflect.Modifier.isPublic;
import static java.util.Locale.ENGLISH;
import static org.fest.util.Preconditions.*;
import static org.fest.util.Strings.quote;
import java.beans.*;
import java.lang.reflect.Method;
/**
* Utility methods related to JavaBeans Introspection .
*
* @author Alex Ruiz
*/
public final class Introspection {
/**
* Returns a {@link PropertyDescriptor} for a property matching the given name in the given object.
*
* @param propertyName the given property name.
* @param target the given object.
* @return a {@code PropertyDescriptor} for a property matching the given name in the given object.
* @throws NullPointerException if the given property name is {@code null}.
* @throws IllegalArgumentException if the given property name is empty.
* @throws NullPointerException if the given object is {@code null}.
* @throws IntrospectionError if a matching property cannot be found or accessed.
*/
public static PropertyDescriptor getProperty(String propertyName, Object target) {
checkNotNullOrEmpty(propertyName);
checkNotNull(target);
BeanInfo beanInfo = null;
Class> type = target.getClass();
try {
beanInfo = Introspector.getBeanInfo(type);
} catch (Throwable t) {
throw new IntrospectionError(format("Unable to get BeanInfo for type %s", type.getName()), t);
}
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
if (propertyName.equals(descriptor.getName())) {
return descriptor;
}
}
throw new IntrospectionError(propertyNotFoundErrorMessage(propertyName, target));
}
private static String propertyNotFoundErrorMessage(String propertyName, Object target) {
String targetTypeName = target.getClass().getName();
String property = quote(propertyName);
Method getter = findGetter(propertyName, target);
if (getter == null) {
return format("No getter for property %s in %s", property, targetTypeName);
}
if (!isPublic(getter.getModifiers())) {
return format("No public getter for property %s in %s", property, targetTypeName);
}
return format("Unable to find property %s in %s", property, targetTypeName);
}
private static Method findGetter(String propertyName, Object target) {
String capitalized = propertyName.substring(0, 1).toUpperCase(ENGLISH) + propertyName.substring(1);
// try to find getProperty
Method getter = findMethod("get" + capitalized, target);
if (getter != null) {
return getter;
}
// try to find isProperty for boolean properties
return findMethod("is" + capitalized, target);
}
private static Method findMethod(String name, Object target) {
// TODO walk class hierarchy to check if any superclass declares the method we are looking for.
try {
return target.getClass().getDeclaredMethod(name);
} catch (Throwable t) {
return null;
}
}
private Introspection() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/IntrospectionError.java 0000664 0000000 0000000 00000002562 12430175307 0025320 0 ustar 00root root 0000000 0000000 /*
* Created on Jun 28, 2010
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* Copyright @2010-2011 the original author or authors.
*/
package org.fest.util;
/**
* Error that occurred when using JavaBeans
* Instrospection .
*
* @author Alex Ruiz
*/
public class IntrospectionError extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* Creates a new {@link IntrospectionError}.
* @param message the detail message.
*/
public IntrospectionError(String message) {
super(message);
}
/**
* Creates a new {@link IntrospectionError}.
* @param message the detail message.
* @param cause the original cause.
*/
public IntrospectionError(String message, Throwable cause) {
super(message, cause);
}
}
fest-util-1.2.5/src/main/java/org/fest/util/Iterables.java 0000664 0000000 0000000 00000005535 12430175307 0023363 0 ustar 00root root 0000000 0000000 /*
* Created on Aug 23, 2012
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2012 the original author or authors.
*/
package org.fest.util;
import static java.util.Collections.emptyList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* Utility methods related to {@code Iterable}s.
*
* @author Yvonne Wang
* @author Alex Ruiz
* @author Joel Costigliola
*/
// TODO(alexRuiz): Get rid of this class.
public final class Iterables {
/**
* Indicates whether the given {@link Iterable} is {@code null} or empty.
*
* @param iterable the given {@code Iterable} to check.
* @return {@code true} if the given {@code Iterable} is {@code null} or empty, otherwise {@code false}.
*/
public static boolean isNullOrEmpty(Iterable> iterable) {
if (iterable == null) {
return true;
}
if (iterable instanceof Collection && ((Collection>) iterable).isEmpty()) {
return true;
}
return !iterable.iterator().hasNext();
}
/**
* Returns the size of the given {@link Iterable}.
*
* @param iterable the {@link Iterable} to get size.
* @return the size of the given {@link Iterable}.
* @throws NullPointerException if given {@link Iterable} is null.
*/
public static int sizeOf(Iterable> iterable) {
if (iterable == null) {
throw new NullPointerException("Iterable must not be null");
}
if (iterable instanceof Collection) {
return ((Collection>) iterable).size();
}
int size = 0;
Iterator> iterator = iterable.iterator();
while (iterator.hasNext()) {
size++;
iterator.next();
}
return size;
}
/**
* Returns all the non-{@code null} elements in the given {@link Iterable}.
*
* @param the type of elements of the {@code Iterable}.
* @param i the given {@code Iterable}.
* @return all the non-{@code null} elements in the given {@code Iterable}. An empty list is returned if the given
* {@code Iterable} is {@code null}.
* @since 1.1.3
*/
public static List nonNullElementsIn(Iterable i) {
if (isNullOrEmpty(i)) {
return emptyList();
}
List nonNull = new ArrayList();
for (T element : i) {
if (element != null) {
nonNull.add(element);
}
}
return nonNull;
}
private Iterables() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/Lists.java 0000664 0000000 0000000 00000004005 12430175307 0022536 0 ustar 00root root 0000000 0000000 /*
* Created on Aug 23, 2012
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2012 the original author or authors.
*/
package org.fest.util;
import java.util.ArrayList;
/**
* Utility methods related to {@code java.util.List}s.
*
* @author Yvonne Wang
* @author Alex Ruiz
* @author Joel Costigliola
*/
public final class Lists {
/**
* Creates a mutable {@link ArrayList} containing the given elements.
*
* @param the generic type of the {@code ArrayList} to create.
* @param elements the elements to store in the {@code ArrayList}.
* @return the created {@code ArrayList}, of {@code null} if the given array of elements is {@code null}.
*/
public static ArrayList newArrayList(T... elements) {
if (elements == null) {
return null;
}
ArrayList list = new ArrayList();
for (T e : elements) {
list.add(e);
}
return list;
}
/**
* Creates a mutable {@link ArrayList} containing the given elements.
*
* @param the generic type of the {@code ArrayList} to create.
* @param elements the elements to store in the {@code ArrayList}.
* @return the created {@code ArrayList}, of {@code null} if the given {@code Iterable} is {@code null}.
*/
public static ArrayList newArrayList(Iterable extends T> elements) {
if (elements == null) {
return null;
}
ArrayList list = new ArrayList();
for (T e : elements) {
list.add(e);
}
return list;
}
private Lists() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/Maps.java 0000664 0000000 0000000 00000004164 12430175307 0022346 0 ustar 00root root 0000000 0000000 /*
* Created on Jan 25, 2008
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2008-2012 the original author or authors.
*/
package org.fest.util;
import static org.fest.util.ToString.toStringOf;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* Utility methods related to maps.
*
* @author Yvonne Wang
* @author Alex Ruiz
*/
public class Maps {
/**
* Indicates whether the given {@code Map} is {@code null} or empty.
*
* @param map the map to check.
* @return {@code true} if the given {@code Map} is {@code null} or empty, otherwise {@code false}.
*/
public static boolean isNullOrEmpty(Map, ?> map) {
return map == null || map.isEmpty();
}
/**
* Returns the {@code String} representation of the given map, or {@code null} if the given map is {@code null}.
*
* @param map the map to format.
* @return the {@code String} representation of the given map.
*/
public static String format(Map, ?> map) {
if (map == null) {
return null;
}
Iterator> i = map.entrySet().iterator();
if (!i.hasNext()) {
return "{}";
}
StringBuilder buffer = new StringBuilder();
buffer.append("{");
for (;;) {
Entry, ?> e = (Entry, ?>) i.next();
buffer.append(format(map, e.getKey()));
buffer.append('=');
buffer.append(format(map, e.getValue()));
if (!i.hasNext()) {
return buffer.append("}").toString();
}
buffer.append(", ");
}
}
private static Object format(Map, ?> map, Object o) {
return o == map ? "(this Map)" : toStringOf(o);
}
private Maps() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/Objects.java 0000664 0000000 0000000 00000006465 12430175307 0023045 0 ustar 00root root 0000000 0000000 /*
* Created on Jun 2, 2006
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2006-2012 the original author or authors.
*/
package org.fest.util;
import static org.fest.util.Arrays.*;
import java.lang.reflect.Array;
/**
* Utility methods related to objects.
*
* @author Alex Ruiz
* @author Joel Costigliola
*/
public final class Objects {
/** Prime number used to calculate the hash code of objects. */
public static final int HASH_CODE_PRIME = 31;
/**
* Returns {@code true} if the given objects are equal or if both objects are {@code null}.
*
* @param o1 one of the objects to compare.
* @param o2 one of the objects to compare.
* @return {@code true} if the given objects are equal or if both objects are {@code null}.
*/
public static boolean areEqual(Object o1, Object o2) {
if (o1 == null) {
return o2 == null;
}
if (o1.equals(o2)) {
return true;
}
return areEqualArrays(o1, o2);
}
private static boolean areEqualArrays(Object o1, Object o2) {
if (!isArray(o1) || !isArray(o2)) {
return false;
}
if (o1 == o2) {
return true;
}
int size = Array.getLength(o1);
if (Array.getLength(o2) != size) {
return false;
}
for (int i = 0; i < size; i++) {
Object e1 = Array.get(o1, i);
Object e2 = Array.get(o2, i);
if (!areEqual(e1, e2)) {
return false;
}
}
return true;
}
/**
* Returns an array containing the names of the given types.
*
* @param types the given types.
* @return the names of the given types stored in an array.
*/
public static String[] namesOf(Class>... types) {
if (isNullOrEmpty(types)) {
return new String[0];
}
String[] names = new String[types.length];
for (int i = 0; i < types.length; i++) {
names[i] = types[i].getName();
}
return names;
}
/**
* Returns the hash code for the given object. If the object is {@code null}, this method returns zero. Otherwise
* calls the method {@code hashCode} of the given object.
*
* @param o the given object.
* @return the hash code for the given object
*/
public static int hashCodeFor(Object o) {
return o != null ? o.hashCode() : 0;
}
/**
* Casts the given object to the given type only if the object is of the given type. If the object is not of the given
* type, this method returns {@code null}.
*
* @param the generic type to cast the given object to.
* @param o the object to cast.
* @param type the given type.
* @return the casted object, or {@code null} if the given object is not to the given type.
*/
public static T castIfBelongsToType(Object o, Class type) {
if (o != null && type.isAssignableFrom(o.getClass())) {
return type.cast(o);
}
return null;
}
private Objects() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/Preconditions.java 0000664 0000000 0000000 00000003351 12430175307 0024263 0 ustar 00root root 0000000 0000000 /*
* Created on Sep 4, 2012
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2012 Google Inc. and others.
*/
package org.fest.util;
/**
* Verifies correct argument values and state. Borrowed from Guava.
*
* @author alruiz@google.com (Alex Ruiz)
*/
public final class Preconditions {
/**
* Verifies that the given {@code String} is not {@code null} or empty.
*
* @param s the given {@code String}.
* @return the validated {@code String}.
* @throws NullPointerException if the given {@code String} is {@code null}.
* @throws IllegalArgumentException if the given {@code String} is empty.
*/
public static String checkNotNullOrEmpty(String s) {
checkNotNull(s);
if (s.isEmpty()) {
throw new IllegalArgumentException();
}
return s;
}
/**
* Verifies that the given object reference is not {@code null}.
*
* @param reference the given object reference.
* @return the non-{@code null} reference that was validated.
* @throws NullPointerException if the given object reference is {@code null}.
*/
public static T checkNotNull(T reference) {
if (reference == null) {
throw new NullPointerException();
}
return reference;
}
private Preconditions() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/Sets.java 0000664 0000000 0000000 00000002670 12430175307 0022364 0 ustar 00root root 0000000 0000000 /*
* Created on Aug 23, 2012
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2012 the original author or authors.
*/
package org.fest.util;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Utility methods related to {@link Set}s.
*
* @author alruiz
*/
public final class Sets {
/**
* Creates a mutable {@link LinkedHashSet} containing the given elements.
*
* @param the generic type of the {@code LinkedHashSet} to create.
* @param elements the elements to store in the {@code LinkedHashSet}.
* @return the created {@code LinkedHashSet}, of {@code null} if the given array of elements is {@code null}.
* @since 1.1.5
*/
public static LinkedHashSet newLinkedHashSet(T... elements) {
if (elements == null) {
return null;
}
LinkedHashSet set = new LinkedHashSet();
for (T e : elements) {
set.add(e);
}
return set;
}
private Sets() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/Strings.java 0000664 0000000 0000000 00000013632 12430175307 0023077 0 ustar 00root root 0000000 0000000 /*
* Created on Jun 2, 2006
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2006-2012 the original author or authors.
*/
package org.fest.util;
/**
* Utility methods related to {@code String}s.
*
* @author Alex Ruiz
*/
public final class Strings {
/**
* Indicates whether the given {@code String} is {@code null} or empty.
*
* @param s the {@code String} to check.
* @return {@code true} if the given {@code String} is {@code null} or empty, otherwise {@code false}.
*/
public static boolean isNullOrEmpty(String s) {
return s == null || s.length() == 0;
}
/**
* Only there for backward compatibility reasons - use {@link #isNullOrEmpty(String)} instead.
*/
@Deprecated
public static boolean isEmpty(String s) {
return isNullOrEmpty( s );
}
/**
* Returns the given {@code String} surrounded by single quotes, or {@code null} if the given {@code String} is
* {@code null}.
*
* @param s the given {@code String}.
* @return the given {@code String} surrounded by single quotes, or {@code null} if the given {@code String} is
* {@code null}.
*/
public static String quote(String s) {
return s != null ? concat("'", s, "'") : null;
}
/**
* Returns the given object surrounded by single quotes, only if the object is a {@code String}.
*
* @param o the given object.
* @return the given object surrounded by single quotes, only if the object is a {@code String}.
* @see #quote(String)
*/
public static Object quote(Object o) {
return o instanceof String ? quote(o.toString()) : o;
}
/**
* Concatenates the given objects into a single {@code String}. This method is more efficient than concatenating using
* "+", since only one {@link StringBuilder}
is created.
*
* @param objects the objects to concatenate.
* @return a {@code String} containing the given objects.
*/
public static String concat(Object... objects) {
if (Arrays.isNullOrEmpty(objects)) {
return null;
}
StringBuilder b = new StringBuilder();
for (Object o : objects) {
b.append(o);
}
return b.toString();
}
/**
* Joins the given {@code String}s using a given delimiter. The following example illustrates proper usage of this
* method:
*
*
* Strings.join("a", "b", "c").with("|")
*
*
* which will result in the {@code String} "a|b|c"
.
*
* @param strings the {@code String}s to join.
* @return an intermediate object that takes a given delimiter and knows how to join the given {@code String}s.
* @see StringsToJoin#with(String)
*/
public static StringsToJoin join(String... strings) {
return new StringsToJoin(strings);
}
/**
* Knows how to join {@code String}s using a given delimiter.
*
* @see Strings#join(String[])
*/
public static class StringsToJoin {
/** The {@code String}s to join. */
private final String[] strings;
/**
* Creates a new {@link StringsToJoin}
.
*
* @param strings the {@code String}s to join.
*/
StringsToJoin(String... strings) {
this.strings = strings;
}
/**
* Specifies the delimeter to use to join {@code String}s.
*
* @param delimeter the delimeter to use.
* @return the {@code String}s joined using the given delimeter.
*/
public String with(String delimeter) {
if (delimeter == null) {
throw new IllegalArgumentException("Delimiter should not be null");
}
if (Arrays.isNullOrEmpty(strings)) {
return "";
}
StringBuilder b = new StringBuilder();
int stringCount = strings.length;
for (int i = 0; i < stringCount; i++) {
String s = strings[i];
b.append(s != null ? s : "");
if (i < stringCount - 1) {
b.append(delimeter);
}
}
return b.toString();
}
}
/**
* Appends a given {@code String} to the given target, only if the target does not end with the given {@code String}
* to append. The following example illustrates proper usage of this method:
*
*
* Strings.append("c").to("ab");
* Strings.append("c").to("abc");
*
*
* resulting in the {@code String} "abc"
for both cases.
*
* @param toAppend the {@code String} to append.
* @return an intermediate object that takes the target {@code String} and knows to append the given {@code String}.
* @see StringToAppend#to(String)
*/
public static StringToAppend append(String toAppend) {
return new StringToAppend(toAppend);
}
/**
* Knows how to append a given {@code String} to the given target, only if the target does not end with the given
* {@code String} to append.
*/
public static class StringToAppend {
private final String toAppend;
StringToAppend(String toAppend) {
this.toAppend = toAppend;
}
/**
* Appends the {@code String} specified in the constructor to the {@code String} passed as argument.
*
* @param s the target {@code String}.
* @return a {@code String} containing the target {@code String} with the given {@code String} to append added to
* the end.
*/
public String to(String s) {
if (!s.endsWith(toAppend)) {
return concat(s, toAppend);
}
return s;
}
}
private Strings() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/SystemProperties.java 0000664 0000000 0000000 00000001774 12430175307 0025013 0 ustar 00root root 0000000 0000000 /*
* Created on Feb 10, 2008
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2008-2012 the original author or authors.
*/
package org.fest.util;
/**
* System properties.
*
* @author Yvonne Wang
*/
public final class SystemProperties {
public static final String LINE_SEPARATOR = lineSeparator();
private static String lineSeparator() {
try {
return System.getProperty("line.separator");
} catch (Exception e) {
return "\n";
}
}
private SystemProperties() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/TextFileWriter.java 0000664 0000000 0000000 00000003236 12430175307 0024366 0 ustar 00root root 0000000 0000000 /*
* Created on Jan 27, 2011
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2011-2012 the original author or authors.
*/
package org.fest.util;
import static org.fest.util.Closeables.closeQuietly;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;
/**
* @author Yvonne Wang
* @author Olivier Michallat
*/
public class TextFileWriter {
private static TextFileWriter INSTANCE = new TextFileWriter();
public static TextFileWriter instance() {
return INSTANCE;
}
public void write(File file, String... content) throws IOException {
write(file, Charset.defaultCharset(), content);
}
public void write(File file, Charset charset, String... content) throws IOException {
PrintWriter writer = null;
try {
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
for (String line : content) {
writer.println(line);
}
} finally {
closeQuietly(writer);
}
}
private TextFileWriter() {
}
}
fest-util-1.2.5/src/main/java/org/fest/util/Throwables.java 0000664 0000000 0000000 00000012220 12430175307 0023550 0 ustar 00root root 0000000 0000000 /*
* Created on Dec 13, 2008
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2008-2012 the original author or authors.
*/
package org.fest.util;
import static org.fest.util.Lists.newArrayList;
import java.util.*;
/**
* Utility methods related to {@link Throwable}
s.
*
* @author Alex Ruiz
*/
public final class Throwables {
/**
* Appends the stack trace of the current thread to the one in the given {@link Throwable}
.
*
* @param t the given {@code Throwable}.
* @param methodToStartFrom the name of the method used as the starting point of the current thread's stack trace.
*/
public static void appendStackTraceInCurentThreadToThrowable(Throwable t, String methodToStartFrom) {
List stackTrace = newArrayList(t.getStackTrace());
stackTrace.addAll(stackTraceInCurrentThread(methodToStartFrom));
t.setStackTrace(stackTrace.toArray(new StackTraceElement[stackTrace.size()]));
}
private static List stackTraceInCurrentThread(String methodToStartFrom) {
List filtered = stackTraceInCurrentThread();
List toRemove = new ArrayList();
for (StackTraceElement e : filtered) {
if (methodToStartFrom.equals(e.getMethodName())) {
break;
}
toRemove.add(e);
}
filtered.removeAll(toRemove);
return filtered;
}
private static List stackTraceInCurrentThread() {
return newArrayList(Thread.currentThread().getStackTrace());
}
/**
* Removes the FEST-related elements from the {@link Throwable}
stack trace that have little value for
* end user. Therefore, instead of seeing this:
*
*
* org.junit.ComparisonFailure: expected:<'[Ronaldo]'> but was:<'[Messi]'>
* at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
* at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
* at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
* at java.lang.reflect.Constructor.newInstance(Constructor.java:501)
* at org.fest.assertions.error.ConstructorInvoker.newInstance(ConstructorInvoker.java:34)
* at org.fest.assertions.error.ShouldBeEqual.newComparisonFailure(ShouldBeEqual.java:111)
* at org.fest.assertions.error.ShouldBeEqual.comparisonFailure(ShouldBeEqual.java:103)
* at org.fest.assertions.error.ShouldBeEqual.newAssertionError(ShouldBeEqual.java:81)
* at org.fest.assertions.internal.Failures.failure(Failures.java:76)
* at org.fest.assertions.internal.Objects.assertEqual(Objects.java:116)
* at org.fest.assertions.api.AbstractAssert.isEqualTo(AbstractAssert.java:74)
* at examples.StackTraceFilterExample.main(StackTraceFilterExample.java:13)
*
*
* We get this:
*
*
* org.junit.ComparisonFailure: expected:<'[Ronaldo]'> but was:<'[Messi]'>
* at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
* at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
* at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
* at examples.StackTraceFilterExample.main(StackTraceFilterExample.java:20)
*
* @param throwable the {@code Throwable} to filter stack trace.
*/
public static void removeFestRelatedElementsFromStackTrace(Throwable throwable) {
List filtered = newArrayList(throwable.getStackTrace());
StackTraceElement previous = null;
for (StackTraceElement element : throwable.getStackTrace()) {
if (element.getClassName().contains("org.fest")) {
filtered.remove(element);
// Handle the case when FEST builds a ComparisonFailure by reflection (see ShouldBeEqual.newAssertionError
// method), the stack trace looks like:
//
// java.lang.reflect.Constructor.newInstance(Constructor.java:501),
// org.fest.assertions.error.ConstructorInvoker.newInstance(ConstructorInvoker.java:34),
//
// We want to remove java.lang.reflect.Constructor.newInstance element because it is related to FEST.
if (previous != null && previous.getClassName().equals("java.lang.reflect.Constructor")
&& element.getClassName().contains("org.fest.assertions.error.ConstructorInvoker")) {
filtered.remove(previous);
}
}
previous = element;
}
StackTraceElement[] newStackTrace = filtered.toArray(new StackTraceElement[filtered.size()]);
throwable.setStackTrace(newStackTrace);
}
private Throwables() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/ToString.java 0000664 0000000 0000000 00000006064 12430175307 0023220 0 ustar 00root root 0000000 0000000 /*
* Created on Oct 7, 2009
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2009-2012 the original author or authors.
*/
package org.fest.util;
import static org.fest.util.Arrays.isArray;
import static org.fest.util.Strings.quote;
import java.io.File;
import java.util.*;
/**
* Obtains the {@code toString} representation of an object.
*
* @author Alex Ruiz
* @author Joel Costigliola
* @author Yvonne Wang
*/
public final class ToString {
/**
* Returns the {@code toString} representation of the given object. It may or not the object's own implementation of
* {@code toString}.
*
* @param o the given object.
* @return the {@code toString} representation of the given object.
*/
public static String toStringOf(Object o) {
if (isArray(o)) {
return Arrays.format(o);
}
if (o instanceof Calendar) {
return toStringOf((Calendar) o);
}
if (o instanceof Class>) {
return toStringOf((Class>) o);
}
if (o instanceof Collection>) {
return toStringOf((Collection>) o);
}
if (o instanceof Date) {
return toStringOf((Date) o);
}
if (o instanceof Float) {
return toStringOf((Float) o);
}
if (o instanceof Long) {
return toStringOf((Long) o);
}
if (o instanceof File) {
return toStringOf((File) o);
}
if (o instanceof Map, ?>) {
return toStringOf((Map, ?>) o);
}
if (o instanceof String) {
return quote((String) o);
}
if (o instanceof Comparator) {
return toStringOf((Comparator>) o);
}
return o == null ? null : o.toString();
}
private static String toStringOf(Comparator> comparator) {
String comparatorSimpleClassName = comparator.getClass().getSimpleName();
return quote(!comparatorSimpleClassName.isEmpty() ? comparatorSimpleClassName : "Anonymous Comparator class");
}
private static String toStringOf(Calendar c) {
return Dates.formatAsDatetime(c);
}
private static String toStringOf(Class> c) {
return c.getCanonicalName();
}
private static String toStringOf(Collection> c) {
return Collections.format(c);
}
private static String toStringOf(Date d) {
return Dates.formatAsDatetime(d);
}
private static String toStringOf(Float f) {
return String.format("%sf", f);
}
private static String toStringOf(Long l) {
return String.format("%sL", l);
}
private static String toStringOf(File f) {
return f.getAbsolutePath();
}
private static String toStringOf(Map, ?> m) {
return Maps.format(m);
}
private ToString() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/TypeFilter.java 0000664 0000000 0000000 00000004070 12430175307 0023531 0 ustar 00root root 0000000 0000000 /*
* Created on Nov 1, 2007
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2007-2011 the original author or authors.
*/
package org.fest.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Filters elements of a collection by their data type.
*
* @param the generic type of the objects returned by the filter.
*
* @author Yvonne Wang
*/
public class TypeFilter implements CollectionFilter {
/**
* Creates a new {@link TypeFilter}
.
*
* @param the generic type of the target type.
* @param type the target type for this filter.
* @return the created filter.
*/
public static TypeFilter byType(Class type) {
return new TypeFilter(type);
}
private final Class type;
TypeFilter(Class type) {
this.type = type;
}
/**
* Filters the given collection by the type specified in this filter.
*
* @param target the collection to filter.
* @return a list containing the filtered elements.
* @throws IllegalArgumentException if the given collection is {@code null}.
*/
@Override
@SuppressWarnings("unchecked")
public List filter(Collection> target) {
if (target == null) {
throw new IllegalArgumentException("The collection to filter should not be null");
}
List filtered = new ArrayList();
for (Object o : target) {
if (o == null) {
continue;
}
if (type.isAssignableFrom(o.getClass())) {
filtered.add(o);
}
}
return (List) filtered;
}
}
fest-util-1.2.5/src/main/java/org/fest/util/Types.java 0000664 0000000 0000000 00000002601 12430175307 0022544 0 ustar 00root root 0000000 0000000 /*
* Created on Oct 7, 2010
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2010-2012 the original author or authors.
*/
package org.fest.util;
import static org.fest.util.Lists.newArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Queue;
import java.util.Set;
/**
* Utilities related to Java data types.
*
* @author Alex Ruiz
*/
public final class Types {
private static final Class>[] PRIMITIVE_TYPES =
{ boolean.class, byte.class, short.class, int.class, long.class, float.class, double.class, char.class };
private static final Class>[] COLLECTION_TYPES = { Collection.class, List.class, Queue.class, Set.class };
public static List> primitiveTypes() {
return newArrayList(PRIMITIVE_TYPES);
}
public static List> collectionTypes() {
return newArrayList(COLLECTION_TYPES);
}
private Types() {}
}
fest-util-1.2.5/src/main/java/org/fest/util/VisibleForTesting.java 0000664 0000000 0000000 00000001603 12430175307 0025043 0 ustar 00root root 0000000 0000000 /*
* Created on Mar 18, 2010
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright @2010-2012 the original author or authors.
*/
package org.fest.util;
/**
* Indicates that the visibility of a type or member has been relaxed to make the code testable. Idea borrowed from
* Google Collections :)
*
* @author Alex Ruiz
*
* @since 1.1.1
*/
public @interface VisibleForTesting {}
fest-util-1.2.5/src/main/java/org/fest/util/package.html 0000664 0000000 0000000 00000001675 12430175307 0023070 0 ustar 00root root 0000000 0000000
General-purpose utilities used by FEST modules.