|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2020 Ruslan Yushchenko |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * |
| 24 | + * For more information, please refer to <http://opensource.org/licenses/MIT> |
| 25 | + */ |
| 26 | + |
| 27 | +package com.github.yruslan.channel |
| 28 | + |
| 29 | +import com.github.yruslan.channel.impl.Selector |
| 30 | + |
| 31 | +import java.time.Instant |
| 32 | +import java.time.Instant.now |
| 33 | +import scala.concurrent.duration.{Duration, MILLISECONDS} |
| 34 | + |
| 35 | +class ChannelDecoratorFilter[T](inputChannel: ReadChannel[T], pred: T => Boolean) extends ChannelDecorator[T](inputChannel) with ReadChannel[T] { |
| 36 | + override def recv(): T = { |
| 37 | + var v = inputChannel.recv() |
| 38 | + var found = pred(v) |
| 39 | + |
| 40 | + while (!found) { |
| 41 | + v = inputChannel.recv() |
| 42 | + found = pred(v) |
| 43 | + } |
| 44 | + v |
| 45 | + } |
| 46 | + |
| 47 | + override def tryRecv(): Option[T] = { |
| 48 | + var valueOpt = inputChannel.tryRecv() |
| 49 | + var found = valueOpt.isEmpty || valueOpt.forall(v => pred(v)) |
| 50 | + |
| 51 | + while (!found) { |
| 52 | + valueOpt = inputChannel.tryRecv() |
| 53 | + found = valueOpt.isEmpty || valueOpt.forall(v => pred(v)) |
| 54 | + } |
| 55 | + valueOpt |
| 56 | + } |
| 57 | + |
| 58 | + override def tryRecv(timeout: Duration): Option[T] = { |
| 59 | + if (timeout == Duration.Zero) { |
| 60 | + return tryRecv() |
| 61 | + } |
| 62 | + |
| 63 | + val timeoutMilli = if (timeout.isFinite) timeout.toMillis else 0L |
| 64 | + val startInstant = Instant.now() |
| 65 | + var valueOpt = inputChannel.tryRecv(timeout) |
| 66 | + var found = valueOpt.isEmpty || valueOpt.forall(v => pred(v)) |
| 67 | + var elapsedTime = java.time.Duration.between(startInstant, now).toMillis |
| 68 | + |
| 69 | + if (found || elapsedTime >= timeoutMilli) { |
| 70 | + valueOpt |
| 71 | + } else { |
| 72 | + while (!found && elapsedTime < timeoutMilli) { |
| 73 | + val newTimeout = Duration(timeoutMilli - elapsedTime, MILLISECONDS) |
| 74 | + valueOpt = inputChannel.tryRecv(newTimeout) |
| 75 | + found = valueOpt.isEmpty || valueOpt.forall(v => pred(v)) |
| 76 | + elapsedTime = java.time.Duration.between(startInstant, now).toMillis |
| 77 | + } |
| 78 | + valueOpt |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + override def recver(action: T => Unit): Selector = inputChannel.recver(t => if (pred(t)) action(t)) |
| 83 | + |
| 84 | + override def fornew(action: T => Unit): Unit = inputChannel.fornew(t => if (pred(t)) action(t)) |
| 85 | + |
| 86 | + override def foreach(action: T => Unit): Unit = inputChannel.foreach(t => if (pred(t)) action(t)) |
| 87 | +} |
0 commit comments