001/* 002 * Copyright 2018 Leibniz-Institut für Analytische Wissenschaften – ISAS – e.V.. 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.mztab2.validation; 017 018import de.isas.mztab2.model.MzTab; 019import de.isas.mztab2.model.ValidationMessage; 020import static de.isas.mztab2.model.ValidationMessage.MessageTypeEnum.ERROR; 021import static de.isas.mztab2.model.ValidationMessage.MessageTypeEnum.INFO; 022import static de.isas.mztab2.model.ValidationMessage.MessageTypeEnum.WARN; 023import java.util.Arrays; 024import java.util.Collection; 025import java.util.List; 026import java.util.stream.Collectors; 027 028/** 029 * Delegating validator implementation that forwards validation to the provided 030 * validator implementations. 031 * 032 * @author nilshoffmann 033 */ 034public class MzTabValidator implements Validator<MzTab> { 035 036 private final List<Validator<MzTab>> validators; 037 038 public MzTabValidator(Validator<MzTab>... validator) { 039 this.validators = Arrays.asList(validator); 040 } 041 042 @Override 043 public List<ValidationMessage> validate(MzTab mzTab) { 044 return validators.stream(). 045 map(validator -> 046 { 047 return validator.validate(mzTab); 048 }). 049 flatMap(Collection::stream). 050 collect(Collectors.toList()); 051 } 052 053 /** 054 * Validate the given mzTab object, filtering messages that are of a lower 055 * level than the given one. Thus, for validationLevel=ERROR, only ERROR 056 * messages will be returned, for WARN, ERROR and WARN messages will be 057 * returned. For INFO, INFO, WARN and ERROR messages will be returned. 058 * 059 * @param mzTab the mzTab object to validate. 060 * @param validationLevel the validation level, used as a message filter. 061 * @param validators the validators to apply in sequence to the mzTab 062 * object. 063 * @return the filtered list of validation messages according to the giben 064 * validationLevel. 065 */ 066 public static List<ValidationMessage> validate(MzTab mzTab, 067 ValidationMessage.MessageTypeEnum validationLevel, 068 Validator<MzTab>... validators) { 069 MzTabValidator validator = new MzTabValidator(validators); 070 return validator.validate(mzTab). 071 stream(). 072 filter(validationMessage -> 073 { 074 switch (validationLevel) { 075 case INFO: 076 return (validationMessage.getMessageType() == INFO || validationMessage.getMessageType() == WARN || validationMessage.getMessageType() == ERROR); 077 case WARN: 078 return (validationMessage.getMessageType() == WARN || validationMessage.getMessageType() == ERROR); 079 case ERROR: 080 return (validationMessage.getMessageType() == ERROR); 081 default: 082 throw new IllegalArgumentException( 083 "Unknown message type for validationLevel '"+validationLevel+"' and message: '" + validationMessage. 084 getMessage() + "'!"); 085 } 086 }). 087 collect(Collectors.toList()); 088 089 } 090 091}