| /*=============================================================================# |
| # Copyright (c) 2021 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.rj.servi; |
| |
| import java.util.concurrent.TimeUnit; |
| |
| import org.apache.commons.pool2.impl.DefaultPooledObject; |
| |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| |
| |
| @NonNullByDefault |
| public class APool2NodeObject extends DefaultPooledObject<APool2NodeHandler> { |
| |
| |
| private volatile long lastAllocatedBeginNanos; |
| private volatile long lastAllocatedEndNanos; |
| |
| private volatile long stateTime; |
| |
| |
| public APool2NodeObject(final APool2NodeHandler object) { |
| super(object); |
| this.stateTime= getCreateTime(); |
| } |
| |
| |
| @Override |
| public synchronized boolean allocate() { |
| if (super.allocate()) { |
| this.lastAllocatedBeginNanos= System.nanoTime(); |
| this.stateTime= System.currentTimeMillis(); |
| return true; |
| } |
| return false; |
| } |
| |
| @Override |
| public synchronized void markReturning() { |
| switch (getState()) { |
| case RETURNING: |
| return; |
| case ALLOCATED: |
| this.lastAllocatedEndNanos= System.nanoTime(); |
| //$FALL-THROUGH$ |
| default: |
| this.stateTime= System.currentTimeMillis(); |
| super.markReturning(); |
| return; |
| } |
| } |
| |
| @Override |
| public synchronized boolean deallocate() { |
| switch (getState()) { |
| case IDLE: |
| return false; |
| case ALLOCATED: |
| this.lastAllocatedEndNanos= System.nanoTime(); |
| //$FALL-THROUGH$ |
| case RETURNING: |
| this.stateTime= System.currentTimeMillis(); |
| return super.deallocate(); |
| default: |
| return false; |
| } |
| } |
| |
| @Override |
| public synchronized void invalidate() { |
| switch (getState()) { |
| case INVALID: |
| return; |
| case ALLOCATED: |
| this.lastAllocatedEndNanos= System.nanoTime(); |
| //$FALL-THROUGH$ |
| default: |
| this.stateTime= System.currentTimeMillis(); |
| super.invalidate(); |
| return; |
| } |
| } |
| |
| |
| public long getStateTime() { |
| return this.stateTime; |
| } |
| |
| @Override |
| public long getActiveTimeMillis() { |
| if (getBorrowedCount() <= 0) { |
| return -1; |
| } |
| final long end= this.lastAllocatedEndNanos; |
| final long begin= this.lastAllocatedBeginNanos; |
| long t= end - begin; |
| if (t < 0) { |
| t= System.nanoTime() - begin; |
| } |
| return TimeUnit.NANOSECONDS.toMillis(t); |
| } |
| |
| } |