-
-
Save jp1017/963ff84f0a1796751b6c6223943eb101 to your computer and use it in GitHub Desktop.
Guava 开发
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.yct.demo.guava; | |
import com.google.common.base.MoreObjects; | |
import com.google.common.base.Objects; | |
import static com.google.common.base.Preconditions.*; | |
import com.google.common.collect.ComparisonChain; | |
import com.google.common.collect.Ordering; | |
public class Employee implements Comparable<Employee> { | |
private String name; | |
private Integer age; | |
private Job job; | |
public Employee() { } | |
public Employee (String name,int age) { | |
this.name = checkNotNull(name, "name 不能为null:", name); | |
checkArgument(age > 20, "age 必须大于 20:",age); | |
this.age = age; | |
} | |
public String getCoordinatesAsText() { | |
// return (getGpsCoordinates() != null) ? getGpsCoordinates() : "DEFAULT_LOCATION"; | |
return MoreObjects.firstNonNull(getGpsCoordinates(), "DEFAULT_LOCATION"); | |
} | |
private String getGpsCoordinates() { | |
// retrieve GPS coordinates from satellite // VERY SLOW!! | |
return null; | |
} | |
public int compareTo(Employee other) { | |
return ComparisonChain | |
.start() | |
// 以英文字母(从a到z)的自然顺序,NULL值放在最后 | |
.compare(this.name, other.name, Ordering.natural().nullsLast()) | |
// 以数字的反序(从大到小),NULL值处于最后 | |
.compare(this.age, other.age, Ordering.natural().reverse().nullsLast()) | |
.compare(this.job, other.job, Ordering.natural().nullsLast()) | |
.result(); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hashCode(name, age, job); | |
} | |
@Override | |
public String toString () { | |
return MoreObjects.toStringHelper(this) | |
.omitNullValues() | |
.add("name", name) | |
.add("age", age) | |
.add("job", job) | |
.toString(); | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Integer getAge() { | |
return age; | |
} | |
public void setAge(Integer age) { | |
this.age = age; | |
} | |
public Job getJob() { | |
return job; | |
} | |
public void setJob(Job job) { | |
this.job = job; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void guavaDemo() { | |
Employee ee = new Employee("Jhon",22); | |
// Employee{name=Jhon, age=22} | |
System.out.println(ee.toString()); | |
// DEFAULT_LOCATION | |
System.out.println(ee.getCoordinatesAsText()); | |
List<Map<String, Object>> maps = Lists.newArrayList(); | |
List<Employee> ees = Lists.newArrayList(); | |
List<String> langs = Lists.newArrayList("中文","English","日本語",null); | |
String lang = Joiner.on("|").useForNull("Unkown").join(langs); | |
// 中文|English|日本語|Unkown | |
System.out.println(lang); | |
// Like Map<Job, Collection<Employee>> | |
Multimap<Job, Employee> multimap = ArrayListMultimap.create(); | |
multimap.put(Job.CEO, new Employee("Tom",45)); | |
multimap.put(Job.DESIGNER, new Employee("Jack",24)); | |
multimap.put(Job.DEVELOPER, new Employee("Alice", 31)); | |
multimap.put(Job.DEVELOPER, new Employee("Jhone", 25)); | |
multimap.put(Job.DEVELOPER, new Employee("Jim", 27)); | |
//Map<Job,List<Employee>> jobs = Multimap. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>18.0</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment