<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ck.test</groupId> <artifactId>CK_Test</artifactId> <version>1.0.0</version> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <framework.bom.version>3.18.0</framework.bom.version> <maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-source-plugin.version>3.0.1</maven-source-plugin.version> <maven-jar-plugin.version>3.0.2</maven-jar-plugin.version> <maven-war-plugin.version>3.0.0</maven-war-plugin.version> <maven-install-plugin.version>2.5.2</maven-install-plugin.version> </properties> <dependencyManagement> <dependencies> <!-- Framework --> <dependency> <groupId>com.XXX.framework</groupId> <artifactId>framework-bom</artifactId> <version>${framework.bom.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> <scope>provided</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>${maven-source-plugin.version}</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>${maven-jar-plugin.version}</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>${maven-war-plugin.version}</version> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.5</version> </plugin> <plugin> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <downloadSources>true</downloadSources> <ajdtVersion>none</ajdtVersion> <additionalConfig> <file> <name>.settings/org.eclipse.jdt.core.prefs</name> <content> org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.source=1.7 org.eclipse.jdt.core.compiler.compliance=1.7 </content> </file> </additionalConfig> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
package com.ck.test.misc; import com.google.common.base.MoreObjects; import com.google.common.base.Objects; import com.google.common.collect.ComparisonChain; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Book implements Comparable<Book> { private String title; private String author; private String publisher; private String isbn; private double price; @Override public int hashCode() { return Objects.hashCode(title, author, publisher, isbn); } @Override public boolean equals(Object obj) { if(this == obj){ return true; } if(obj == null){ return false; } if(getClass() != obj.getClass()){ return false; } if(!(obj instanceof Book)){ return false; } Book other = (Book)obj; return Objects.equal(title, other.getTitle()) && Objects.equal(author, other.getAuthor()) && Objects.equal(publisher, other.getPublisher()) && Objects.equal(isbn, other.getIsbn()) && price == other.getPrice(); } @Override public String toString() { return MoreObjects.toStringHelper(this).omitNullValues() .add("title", title) .add("author", author) .add("publisher", publisher) .add("isbn", isbn) .add("price", price) .toString(); } public static void main(String[] args){ Book book = new Book("Core Java", "Tom", "JackMa", "11-22-33-44", 12.80); System.out.println(book); } @Override public int compareTo(Book o) { return ComparisonChain.start() .compare(this.title, o.getTitle()) .compare(this.author, o.getAuthor()) .compare(this.publisher, o.publisher) .compare(this.isbn, o.getIsbn()) .compare(this.price, o.getPrice()) .result(); } }