content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org and other contributors.                    ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.core.center.queue;
  
  import java.io.Serial;
  import java.util.Collection;
  import java.util.concurrent.LinkedBlockingQueue;
  import java.util.concurrent.TimeUnit;
  import java.util.function.Predicate;
  
  /**
   * 自定义加入前检查的{@link LinkedBlockingQueue}，给定一个检查函数，在加入元素前检查此函数 原理是通过Runtime#freeMemory()获取剩余内存，当剩余内存低于指定的阈值时，不再加入。
   *
   * @param <E> 对象类型
   * @author Kimi Liu
   * @since Java 17+
   */
  public class CheckedLinkedBlockingQueue<E> extends LinkedBlockingQueue<E> {
  
      @Serial
      private static final long serialVersionUID = 2852279169056L;
  
      /**
       * 检查函数
       */
      protected final Predicate<E> checker;
  
      /**
       * 构造
       *
       * @param checker 检查函数
       */
      public CheckedLinkedBlockingQueue(final Predicate<E> checker) {
          super(Integer.MAX_VALUE);
          this.checker = checker;
      }
  
      /**
       * 构造
       *
       * @param c       初始集合
       * @param checker 检查函数
       */
      public CheckedLinkedBlockingQueue(final Collection<? extends E> c, final Predicate<E> checker) {
          super(c);
          this.checker = checker;
      }
  
      @Override
      public void put(final E e) throws InterruptedException {
          if (checker.test(e)) {
              super.put(e);
          }
      }
  
      @Override
      public boolean offer(final E e, final long timeout, final TimeUnit unit) throws InterruptedException {
          return checker.test(e) && super.offer(e, timeout, unit);
      }
  
      @Override
      public boolean offer(final E e) {
          return checker.test(e) && super.offer(e);
      }
  
  }
  