1 /*
   2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.collections;
  27 
  28 import java.util.Arrays;
  29 import java.util.List;
  30 import java.util.stream.Collectors;
  31 import javafx.beans.Observable;
  32 import javafx.beans.property.StringProperty;
  33 import javafx.beans.property.StringPropertyBase;
  34 
  35 public class Person implements Comparable<Person> {
  36     public StringProperty name = new StringPropertyBase("foo") {
  37 
  38         @Override
  39         public Object getBean() {
  40             return Person.this;
  41         }
  42 
  43         @Override
  44         public String getName() {
  45             return "name";
  46         }
  47     };
  48 
  49     public Person(String name) {
  50         this.name.set(name);
  51     }
  52     
  53     public Person() {
  54     }
  55 
  56     @Override
  57     public boolean equals(Object obj) {
  58         if (obj == null) {
  59             return false;
  60         }
  61         if (getClass() != obj.getClass()) {
  62             return false;
  63         }
  64         final Person other = (Person) obj;
  65         if (this.name.get() != other.name.get() && (this.name.get() == null || !this.name.get().equals(other.name.get()))) {
  66             return false;
  67         }
  68         return true;
  69     }
  70 
  71     @Override
  72     public int hashCode() {
  73         int hash = 7;
  74         hash = 59 * hash + (this.name.get() != null ? this.name.get().hashCode() : 0);
  75         return hash;
  76     }
  77 
  78     @Override
  79     public String toString() {
  80         return "Person[" + name.get() + "]";
  81     }
  82 
  83     @Override
  84     public int compareTo(Person o) {
  85         return this.name.get().compareTo(o.name.get());
  86     }
  87     
  88     public static ObservableList<Person> createPersonsList(Person... persons) {
  89         ObservableList<Person> list = FXCollections.observableArrayList(
  90                 (Person p) -> new Observable[]{p.name});
  91         list.addAll(persons);
  92         return list;
  93     }
  94 
  95     public static List<Person> createPersonsFromNames(String... names) {
  96         return Arrays.asList(names).stream().
  97                 map(name -> new Person(name)).collect(Collectors.toList());
  98     }
  99 
 100     public static ObservableList<Person> createPersonsList(String... names) {
 101         ObservableList<Person> list = FXCollections.observableArrayList(
 102                 (Person p) -> new Observable[]{p.name});
 103         list.addAll(createPersonsFromNames(names));
 104         return list;
 105     }
 106 }