001/* 002 * Copyright 2020 nils.hoffmann. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.isas.lipidomics.domain; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.Collections; 021import java.util.Comparator; 022import java.util.Iterator; 023import java.util.List; 024import java.util.ListIterator; 025import java.util.Spliterator; 026import java.util.function.Consumer; 027import java.util.function.IntFunction; 028import java.util.function.Predicate; 029import java.util.function.UnaryOperator; 030import java.util.stream.Collectors; 031import java.util.stream.Stream; 032import lombok.EqualsAndHashCode; 033import org.apache.commons.lang3.tuple.Pair; 034 035/** 036 * 037 * @author nils.hoffmann 038 */ 039@EqualsAndHashCode 040public class ModificationsList implements List<Pair<Integer, String>> { 041 042 protected static final ModificationsList NONE = new ModificationsList(Collections.emptyList()); 043 044 public ModificationsList() { 045 this.al = new ArrayList<>(); 046 } 047 048 public ModificationsList(List<Pair<Integer, String>> list) { 049 this.al = list; 050 } 051 052 private final List<Pair<Integer, String>> al; 053 054 @Override 055 public int size() { 056 return al.size(); 057 } 058 059 @Override 060 public boolean isEmpty() { 061 return al.isEmpty(); 062 } 063 064 @Override 065 public boolean contains(Object o) { 066 return al.contains(o); 067 } 068 069 @Override 070 public int indexOf(Object o) { 071 return al.indexOf(o); 072 } 073 074 @Override 075 public int lastIndexOf(Object o) { 076 return al.lastIndexOf(o); 077 } 078 079 @Override 080 public Object[] toArray() { 081 return al.toArray(); 082 } 083 084 @Override 085 public <T> T[] toArray(T[] a) { 086 return al.toArray(a); 087 } 088 089 @Override 090 public Pair<Integer, String> get(int index) { 091 return al.get(index); 092 } 093 094 @Override 095 public Pair<Integer, String> set(int index, Pair<Integer, String> element) { 096 return al.set(index, element); 097 } 098 099 @Override 100 public boolean add(Pair<Integer, String> e) { 101 return al.add(e); 102 } 103 104 @Override 105 public void add(int index, Pair<Integer, String> element) { 106 al.add(index, element); 107 } 108 109 @Override 110 public Pair<Integer, String> remove(int index) { 111 return al.remove(index); 112 } 113 114 @Override 115 public boolean remove(Object o) { 116 return al.remove(o); 117 } 118 119 @Override 120 public void clear() { 121 al.clear(); 122 } 123 124 @Override 125 public boolean addAll(Collection<? extends Pair<Integer, String>> c) { 126 return al.addAll(c); 127 } 128 129 @Override 130 public boolean addAll(int index, Collection<? extends Pair<Integer, String>> c) { 131 return al.addAll(index, c); 132 } 133 134 @Override 135 public boolean removeAll(Collection<?> c) { 136 return al.removeAll(c); 137 } 138 139 @Override 140 public boolean retainAll(Collection<?> c) { 141 return al.retainAll(c); 142 } 143 144 @Override 145 public ListIterator<Pair<Integer, String>> listIterator(int index) { 146 return al.listIterator(index); 147 } 148 149 @Override 150 public ListIterator<Pair<Integer, String>> listIterator() { 151 return al.listIterator(); 152 } 153 154 @Override 155 public Iterator<Pair<Integer, String>> iterator() { 156 return al.iterator(); 157 } 158 159 @Override 160 public List<Pair<Integer, String>> subList(int fromIndex, int toIndex) { 161 return al.subList(fromIndex, toIndex); 162 } 163 164 @Override 165 public void forEach(Consumer<? super Pair<Integer, String>> action) { 166 al.forEach(action); 167 } 168 169 @Override 170 public Spliterator<Pair<Integer, String>> spliterator() { 171 return al.spliterator(); 172 } 173 174 @Override 175 public boolean removeIf(Predicate<? super Pair<Integer, String>> filter) { 176 return al.removeIf(filter); 177 } 178 179 @Override 180 public void replaceAll(UnaryOperator<Pair<Integer, String>> operator) { 181 al.replaceAll(operator); 182 } 183 184 @Override 185 public void sort(Comparator<? super Pair<Integer, String>> c) { 186 al.sort(c); 187 } 188 189 @Override 190 public boolean containsAll(Collection<?> c) { 191 return al.containsAll(c); 192 } 193 194 @Override 195 public String toString() { 196 return al.toString(); 197 } 198 199 @Override 200 public <T> T[] toArray(IntFunction<T[]> generator) { 201 return al.toArray(generator); 202 } 203 204 @Override 205 public Stream<Pair<Integer, String>> stream() { 206 return al.stream(); 207 } 208 209 @Override 210 public Stream<Pair<Integer, String>> parallelStream() { 211 return al.parallelStream(); 212 } 213 214 /** 215 * Returns the total count (number of elements in this list) with that 216 * particular key. 217 * 218 * @param modification the modification to count, e.g. "OH". 219 * @return the number of times this modification occurs, or zero. 220 */ 221 public Integer countFor(String modification) { 222 return this.stream().filter((pair) -> { 223 return pair.getValue().startsWith(modification); 224 }).collect(Collectors.counting()).intValue(); 225 } 226 227 /** 228 * Returns the sum of the occurrence count for modifications with name "OH" 229 * in the provided modifications list. 230 * 231 * @return the sum of the occurrences of OH. 232 */ 233 public Integer countForHydroxy() { 234 return countFor("OH"); 235 } 236 237 /** 238 * Returns the modification names stored in this modifications list. 239 * 240 * @return the modification names. 241 */ 242 public List<String> keys() { 243 return this.stream().map((t) -> { 244 return t.getValue(); 245 }).collect(Collectors.toList()); 246 } 247 248}