github.com/searKing/golang/go@v1.2.74/util/function/consumer/consumer.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // A sequence of elements supporting sequential and parallel aggregate 6 // operations. The following example illustrates an aggregate operation using 7 // SEE java/util/function/Consumer.java 8 package consumer 9 10 import ( 11 "github.com/searKing/golang/go/error/exception" 12 "github.com/searKing/golang/go/util/class" 13 "github.com/searKing/golang/go/util/object" 14 ) 15 16 /** 17 * Represents an operation that accepts a single input argument and returns no 18 * result. Unlike most other functional interfaces, {@code Consumer} is expected 19 * to operate via side-effects. 20 * 21 * <p>This is a <a href="package-summary.html">functional interface</a> 22 * whose functional method is {@link #accept(Object)}. 23 * 24 * @param <T> the type of the input to the operation 25 * 26 * @since 1.8 27 */ 28 type Consumer interface { 29 /** 30 * Performs this operation on the given argument. 31 * 32 * @param t the input argument 33 */ 34 Accept(t interface{}) 35 36 /** 37 * Returns a composed {@code Consumer} that performs, in sequence, this 38 * operation followed by the {@code after} operation. If performing either 39 * operation throws an exception, it is relayed to the caller of the 40 * composed operation. If performing this operation throws an exception, 41 * the {@code after} operation will not be performed. 42 * 43 * @param after the operation to perform after this operation 44 * @return a composed {@code Consumer} that performs in sequence this 45 * operation followed by the {@code after} operation 46 * @throws NullPointerException if {@code after} is null 47 */ 48 AndThen(after Consumer) Consumer 49 } 50 51 type ConsumerFunc func(t interface{}) 52 53 // Accept calls f(t). 54 func (f ConsumerFunc) Accept(t interface{}) { 55 f(t) 56 } 57 58 func (f ConsumerFunc) AndThen(after Consumer) Consumer { 59 object.RequireNonNil(after) 60 return ConsumerFunc(func(t interface{}) { 61 f.Accept(t) 62 after.Accept(t) 63 }) 64 } 65 66 type TODO struct { 67 class.Class 68 } 69 70 func (consumer *TODO) Accept(t interface{}) { 71 panic(exception.NewIllegalStateException1("called wrong Accept method")) 72 } 73 74 func (consumer *TODO) AndThen(after Consumer) Consumer { 75 object.RequireNonNil(after) 76 return ConsumerFunc(func(t interface{}) { 77 consumer.GetDerived().(Consumer).Accept(t) 78 after.Accept(t) 79 }) 80 }