View Javadoc
1   package fr.ifremer.tutti.persistence.entities.data;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import org.apache.commons.lang3.builder.EqualsBuilder;
28  
29  import java.util.Collections;
30  import java.util.Comparator;
31  import java.util.List;
32  import java.util.Objects;
33  
34  public class FishingOperations extends AbstractFishingOperations {
35  
36      public static final String PROPERTY_GEAR_SHOOTING_START_DAY = "gearShootingStartDay";
37  
38      public static final String PROPERTY_GEAR_SHOOTING_START_TIME = "gearShootingStartTime";
39  
40      public static final String PROPERTY_GEAR_SHOOTING_END_DAY = "gearShootingEndDay";
41  
42      public static final String PROPERTY_GEAR_SHOOTING_END_TIME = "gearShootingEndTime";
43  
44      public static boolean equals(FishingOperation fishingOperation1, FishingOperation fishingOperation2) {
45  
46          EqualsBuilder equalsBuilder = new EqualsBuilder();
47  
48  //        Date day1 = DateUtil.getDay(fishingOperation1.getGearShootingStartDate());
49  //        Date day2 = DateUtil.getDay(fishingOperation2.getGearShootingStartDate());
50  //
51  //        equalsBuilder.append(day1, day2);
52          equalsBuilder.append(fishingOperation1.getStationNumber(), fishingOperation2.getStationNumber());
53          equalsBuilder.append(fishingOperation1.getFishingOperationNumber(), fishingOperation2.getFishingOperationNumber());
54          equalsBuilder.append(fishingOperation1.getMultirigAggregation(), fishingOperation2.getMultirigAggregation());
55  
56          return equalsBuilder.isEquals();
57  
58      }
59  
60      public static boolean equalsNaturalId(FishingOperation fishingOperation1, String naturalId2) {
61  
62          String naturalId1 = getNaturalId(fishingOperation1);
63          return Objects.equals(naturalId1, naturalId2);
64  
65      }
66  
67      public static String getNaturalId(FishingOperation fishingOperation) {
68  
69          String cruiseId = Cruises.getNaturalId(fishingOperation.getCruise());
70          return cruiseId + "--" + fishingOperation.getStationNumber() + "--" + fishingOperation.getFishingOperationNumber() + "--" + fishingOperation.getMultirigAggregation();
71  
72      }
73  
74      public static void sort(List<FishingOperation> cruises) {
75          Collections.sort(cruises, FISHING_OPERATION_COMPARATOR);
76      }
77  
78      public static Comparator<FishingOperation> FISHING_OPERATION_COMPARATOR = new Comparator<FishingOperation>() {
79          @Override
80          public int compare(FishingOperation o1, FishingOperation o2) {
81  
82              int result = compare(o1.getGearShootingStartDate(), o2.getGearShootingStartDate());
83              if (result == 0) {
84                  result = compare(o1.getStationNumber(), o2.getStationNumber());
85              }
86              if (result == 0) {
87                  result = compare(o1.getFishingOperationNumber(), o2.getFishingOperationNumber());
88              }
89  
90              return result;
91          }
92  
93          private <C extends Comparable<C>> int compare(C o1, C o2) {
94              int result;
95              if (Objects.equals(o1, o2)) {
96                  result = 0;
97              } else if (o1 == null) {
98                  result = -1;
99              } else if (o2 == null) {
100                 result = 1;
101             } else {
102                 result = o1.compareTo(o2);
103             }
104 
105             return result;
106         }
107     };
108 
109 }