001/* 002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Kevin Leturc <kleturc@nuxeo.com> 018 * 019 */ 020package org.nuxeo.ecm.platform.audit.api; 021 022import java.util.stream.Collectors; 023import java.util.stream.Stream; 024import java.util.stream.StreamSupport; 025 026import org.nuxeo.ecm.core.query.sql.model.LiteralList; 027import org.nuxeo.ecm.core.query.sql.model.Literals; 028import org.nuxeo.ecm.core.query.sql.model.Operator; 029import org.nuxeo.ecm.core.query.sql.model.Predicate; 030import org.nuxeo.ecm.core.query.sql.model.Reference; 031 032/** 033 * Predicate builders for audit 034 * 035 * @since 9.3 036 */ 037public class Predicates { 038 039 private Predicates() { 040 // no instantiation allowed 041 } 042 043 public static Predicate eq(String name, Object value) { 044 return createPredicate(name, Operator.EQ, value); 045 } 046 047 public static Predicate lt(String name, Object value) { 048 return createPredicate(name, Operator.LT, value); 049 } 050 051 public static Predicate lte(String name, Object value) { 052 return createPredicate(name, Operator.LTEQ, value); 053 } 054 055 public static Predicate gte(String name, Object value) { 056 return createPredicate(name, Operator.GTEQ, value); 057 } 058 059 public static Predicate gt(String name, Object value) { 060 return createPredicate(name, Operator.GT, value); 061 } 062 063 public static Predicate startsWith(String name, Object value) { 064 return createPredicate(name, Operator.STARTSWITH, value); 065 } 066 067 public static Predicate in(String name, Iterable<?> values) { 068 return createPredicate(name, Operator.IN, StreamSupport.stream(values.spliterator(), false)); 069 } 070 071 public static <T> Predicate in(String name, T value, T... values) { 072 return createPredicate(name, Operator.IN, Stream.concat(Stream.of(value), Stream.of(values))); 073 } 074 075 public static Predicate in(String name, Object[] values) { 076 return createPredicate(name, Operator.IN, Stream.of(values)); 077 } 078 079 private static Predicate createPredicate(String name, Operator operator, Object value) { 080 return new Predicate(new Reference(name), operator, Literals.toLiteral(value)); 081 } 082 083 private static Predicate createPredicate(String name, Operator operator, Stream<?> values) { 084 return new Predicate(new Reference(name), operator, 085 values.map(Literals::toLiteral).collect(Collectors.toCollection(LiteralList::new))); 086 } 087 088}