Guava Basic Utilities

check string with null or empty value - Strings.isNullOrEmpty(str)

package org.fool.test;

import com.google.common.base.Strings;

public class StringTest {
	public static void main(String[] args) {
		String var1 = null;
		String var2 = "";
		String var3 = " ";
		String var4 = "     \t\t\t";
		String var5 = "\t\r\n";
		String var6 = "Hello World";

		System.out.println("var1 is blank ? = " + Strings.isNullOrEmpty(var1)); // true
		System.out.println("var2 is blank ? = " + Strings.isNullOrEmpty(var2)); // true
		System.out.println("var3 is blank ? = " + Strings.isNullOrEmpty(var3)); // false
		System.out.println("var4 is blank ? = " + Strings.isNullOrEmpty(var4)); // false
		System.out.println("var5 is blank ? = " + Strings.isNullOrEmpty(var5)); // false
		System.out.println("var6 is blank ? = " + Strings.isNullOrEmpty(var6)); // false
	}
}

JavaBean

public class Book implements Comparable<Book> {
	private String author;
	private String title;
	private String publisher;
	private String isbn;
	private double price;
        // getter/setter
	...
}

override hashCode

@Override
public int hashCode() {
	return Objects.hashCode(title, author, publisher, isbn);
}

override equals

@Override
public boolean equals(Object obj) {
	if (this == obj) {
		return true;
	}
	if (obj == null || getClass() != obj.getClass()) {
		return false;
	}

	final Book other = (Book) obj;

	return Objects.equal(this.author, other.author) 
			&& Objects.equal(this.title, other.title)
			&& Objects.equal(this.publisher, other.publisher) 
			&& Objects.equal(this.isbn, other.isbn)
			&& Objects.equal(this.price, other.price);
}

override toString

@Override
public String toString() {
	return MoreObjects.toStringHelper(this)
			.omitNullValues()
			.add("title", title)
			.add("author", author)
			.add("publisher", publisher)
			.add("price", price)
			.add("isbn", isbn)
			.toString();
}

override compareTo

@Override
public int compareTo(Book o) {
	return ComparisonChain.start()
			.compare(this.title, o.getTitle())
			.compare(this.author, o.getAuthor())
			.compare(this.publisher, o.getPublisher())
			.compare(this.isbn, o.getIsbn())
			.compare(this.price, o.getPrice())
			.result();
}

  

猜你喜欢

转载自agilestyle.iteye.com/blog/2288931