import java.time.LocalDate ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Comparator ;
import java.util.List ;
public class Main {
public static void main
( String [ ] args
) { Objeto o1 = new Objeto( 1 , "c" , LocalDate.of ( 2018 , 01, 1 ) ) ;
Objeto o2 = new Objeto( 2 , "b" , null ) ;
Objeto o3 = new Objeto( 3 , "a" , LocalDate.of ( 2018 , 01, 2 ) ) ;
List< Objeto> objetos = new ArrayList<> ( ) ;
objetos.add ( o1) ;
objetos.add ( o2) ;
objetos.add ( o3) ;
System .
out .
println ( "SEM ORDENAÇÃO: " ) ; for ( Objeto objeto : objetos) {
}
System .
out .
println ( "\n \n ORDENADO PELO NÚMERO: " ) ; Collections .
sort ( objetos,
new ObjetoNumeroComparator
( ) ) ; for ( Objeto objeto : objetos) {
}
System .
out .
println ( "\n \n ORDENADO PELA PALAVRA: " ) ; Collections .
sort ( objetos,
new ObjetoPalavraComparator
( ) ) ; for ( Objeto objeto : objetos) {
}
System .
out .
println ( "\n \n ORDENADO PELA DATA: " ) ; for ( Objeto objeto : objetos) {
}
}
}
final class Objeto {
private final int numero;
private final LocalDate data;
public Objeto
( int numero,
String palavra, LocalDate data
) { this .numero = numero;
this .palavra = palavra;
this .data = data;
}
public int getNumero( ) {
return numero;
}
return palavra;
}
public LocalDate getData( ) {
return data;
}
@Override
return numero + " " + palavra + " " + data;
}
}
class ObjetoNumeroComparator implements Comparator< Objeto> {
public ObjetoNumeroComparator( ) {
}
@Override
public int compare( Objeto o1, Objeto o2) {
return o1.getNumero ( ) - o2.getNumero ( ) ;
}
}
class ObjetoPalavraComparator implements Comparator< Objeto> {
public ObjetoPalavraComparator( ) {
}
@Override
public int compare( Objeto o1, Objeto o2) {
return a == null && b == null ? 0
: a == null ? 1
: b == null ? - 1
: a.compareTo ( b) ;
}
}
class ObjetoDataComparator implements Comparator< Objeto> {
public ObjetoDataComparator( ) {
}
@Override
public int compare( Objeto o1, Objeto o2) {
LocalDate a = o1.getData ( ) ;
LocalDate b = o2.getData ( ) ;
return a == null && b == null ? 0
: a == null ? 1
: b == null ? - 1
: a.compareTo ( b) ;
}
}
aW1wb3J0IGphdmEudGltZS5Mb2NhbERhdGU7CmltcG9ydCBqYXZhLnV0aWwuQXJyYXlMaXN0OwppbXBvcnQgamF2YS51dGlsLkNvbGxlY3Rpb25zOwppbXBvcnQgamF2YS51dGlsLkNvbXBhcmF0b3I7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKCnB1YmxpYyBjbGFzcyBNYWluIHsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBPYmpldG8gbzEgPSBuZXcgT2JqZXRvKDEsICJjIiwgTG9jYWxEYXRlLm9mKDIwMTgsIDAxLCAxKSk7CiAgICAgICAgT2JqZXRvIG8yID0gbmV3IE9iamV0bygyLCAiYiIsIG51bGwpOwogICAgICAgIE9iamV0byBvMyA9IG5ldyBPYmpldG8oMywgImEiLCBMb2NhbERhdGUub2YoMjAxOCwgMDEsIDIpKTsKICAgIAogICAgICAgIExpc3Q8T2JqZXRvPiBvYmpldG9zID0gbmV3IEFycmF5TGlzdDw+KCk7CiAgICAgICAgb2JqZXRvcy5hZGQobzEpOwogICAgICAgIG9iamV0b3MuYWRkKG8yKTsKICAgICAgICBvYmpldG9zLmFkZChvMyk7CiAgICAKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlNFTSBPUkRFTkHDh8ODTzogIik7CiAgICAgICAgZm9yIChPYmpldG8gb2JqZXRvIDogb2JqZXRvcykgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ob2JqZXRvKTsKICAgICAgICB9CiAgICAKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlxuXG5PUkRFTkFETyBQRUxPIE7Dmk1FUk86ICIpOwogICAgICAgIENvbGxlY3Rpb25zLnNvcnQob2JqZXRvcywgbmV3IE9iamV0b051bWVyb0NvbXBhcmF0b3IoKSk7CiAgICAgICAgZm9yIChPYmpldG8gb2JqZXRvIDogb2JqZXRvcykgewogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ob2JqZXRvKTsKICAgICAgICB9CiAgICAKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlxuXG5PUkRFTkFETyBQRUxBIFBBTEFWUkE6ICIpOwogICAgICAgIENvbGxlY3Rpb25zLnNvcnQob2JqZXRvcywgbmV3IE9iamV0b1BhbGF2cmFDb21wYXJhdG9yKCkpOwogICAgICAgIGZvciAoT2JqZXRvIG9iamV0byA6IG9iamV0b3MpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKG9iamV0byk7CiAgICAgICAgfQogICAgCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJcblxuT1JERU5BRE8gUEVMQSBEQVRBOiAiKTsKICAgICAgICBDb2xsZWN0aW9ucy5zb3J0KG9iamV0b3MsIG5ldyBPYmpldG9EYXRhQ29tcGFyYXRvcigpKTsKICAgICAgICBmb3IgKE9iamV0byBvYmpldG8gOiBvYmpldG9zKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihvYmpldG8pOwogICAgICAgIH0KICAgIH0KfQoKZmluYWwgY2xhc3MgT2JqZXRvIHsKICAgIHByaXZhdGUgZmluYWwgaW50IG51bWVybzsKICAgIHByaXZhdGUgZmluYWwgU3RyaW5nIHBhbGF2cmE7CiAgICBwcml2YXRlIGZpbmFsIExvY2FsRGF0ZSBkYXRhOwogICAgCiAgICBwdWJsaWMgT2JqZXRvKGludCBudW1lcm8sIFN0cmluZyBwYWxhdnJhLCBMb2NhbERhdGUgZGF0YSkgewogICAgICAgIHRoaXMubnVtZXJvID0gbnVtZXJvOwogICAgICAgIHRoaXMucGFsYXZyYSA9IHBhbGF2cmE7CiAgICAgICAgdGhpcy5kYXRhID0gZGF0YTsKICAgIH0KCiAgICBwdWJsaWMgaW50IGdldE51bWVybygpIHsKICAgICAgICByZXR1cm4gbnVtZXJvOwogICAgfQoKICAgIHB1YmxpYyBTdHJpbmcgZ2V0UGFsYXZyYSgpIHsKICAgICAgICByZXR1cm4gcGFsYXZyYTsKICAgIH0KCiAgICBwdWJsaWMgTG9jYWxEYXRlIGdldERhdGEoKSB7CiAgICAgICAgcmV0dXJuIGRhdGE7CiAgICB9CgogICAgQE92ZXJyaWRlCiAgICBwdWJsaWMgU3RyaW5nIHRvU3RyaW5nKCkgewogICAgICAgICByZXR1cm4gbnVtZXJvICsgIiAiICsgcGFsYXZyYSArICIgIiArIGRhdGE7CiAgICB9Cn0KCmNsYXNzIE9iamV0b051bWVyb0NvbXBhcmF0b3IgaW1wbGVtZW50cyBDb21wYXJhdG9yPE9iamV0bz4gewogICAgcHVibGljIE9iamV0b051bWVyb0NvbXBhcmF0b3IoKSB7CiAgICB9CgogICAgQE92ZXJyaWRlCiAgICBwdWJsaWMgaW50IGNvbXBhcmUoT2JqZXRvIG8xLCBPYmpldG8gbzIpIHsKICAgICAgICByZXR1cm4gbzEuZ2V0TnVtZXJvKCkgLSBvMi5nZXROdW1lcm8oKTsKICAgIH0KfQoKY2xhc3MgT2JqZXRvUGFsYXZyYUNvbXBhcmF0b3IgaW1wbGVtZW50cyBDb21wYXJhdG9yPE9iamV0bz4gewogICAgcHVibGljIE9iamV0b1BhbGF2cmFDb21wYXJhdG9yKCkgewogICAgfQoKICAgIEBPdmVycmlkZQogICAgcHVibGljIGludCBjb21wYXJlKE9iamV0byBvMSwgT2JqZXRvIG8yKSB7CiAgICAgICAgU3RyaW5nIGEgPSBvMS5nZXRQYWxhdnJhKCk7CiAgICAgICAgU3RyaW5nIGIgPSBvMi5nZXRQYWxhdnJhKCk7CiAgICAgICAgcmV0dXJuIGEgPT0gbnVsbCAmJiBiID09IG51bGwgPyAwCiAgICAgICAgICAgICAgICA6IGEgPT0gbnVsbCA/IDEKICAgICAgICAgICAgICAgIDogYiA9PSBudWxsID8gLTEKICAgICAgICAgICAgICAgIDogYS5jb21wYXJlVG8oYik7CiAgICB9Cn0KCmNsYXNzIE9iamV0b0RhdGFDb21wYXJhdG9yIGltcGxlbWVudHMgQ29tcGFyYXRvcjxPYmpldG8+IHsKICAgIHB1YmxpYyBPYmpldG9EYXRhQ29tcGFyYXRvcigpIHsKICAgIH0KCiAgICBAT3ZlcnJpZGUKICAgIHB1YmxpYyBpbnQgY29tcGFyZShPYmpldG8gbzEsIE9iamV0byBvMikgewogICAgICAgIExvY2FsRGF0ZSBhID0gbzEuZ2V0RGF0YSgpOwogICAgICAgIExvY2FsRGF0ZSBiID0gbzIuZ2V0RGF0YSgpOwogICAgICAgIHJldHVybiBhID09IG51bGwgJiYgYiA9PSBudWxsID8gMAogICAgICAgICAgICAgICAgOiBhID09IG51bGwgPyAxCiAgICAgICAgICAgICAgICA6IGIgPT0gbnVsbCA/IC0xCiAgICAgICAgICAgICAgICA6IGEuY29tcGFyZVRvKGIpOwogICAgfQp9