
com.hazelcast.concurrent.countdownlatch.CountDownLatchProxy Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.concurrent.countdownlatch;
import com.hazelcast.concurrent.countdownlatch.operations.AwaitOperation;
import com.hazelcast.concurrent.countdownlatch.operations.CountDownOperation;
import com.hazelcast.concurrent.countdownlatch.operations.GetCountOperation;
import com.hazelcast.concurrent.countdownlatch.operations.SetCountOperation;
import com.hazelcast.core.ICountDownLatch;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.spi.AbstractDistributedObject;
import com.hazelcast.spi.InternalCompletableFuture;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.Operation;
import com.hazelcast.spi.OperationService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static com.hazelcast.util.ExceptionUtil.rethrowAllowInterrupted;
import static com.hazelcast.util.Preconditions.checkNotNegative;
import static com.hazelcast.util.Preconditions.checkNotNull;
public class CountDownLatchProxy extends AbstractDistributedObject implements ICountDownLatch {
private final String name;
private final int partitionId;
public CountDownLatchProxy(String name, NodeEngine nodeEngine) {
super(nodeEngine, null);
this.name = name;
Data nameAsPartitionAwareData = getNameAsPartitionAwareData();
partitionId = nodeEngine.getPartitionService().getPartitionId(nameAsPartitionAwareData);
}
@Override
public String getName() {
return name;
}
@Override
public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
checkNotNull(unit, "unit can't be null");
AwaitOperation op = new AwaitOperation(name, unit.toMillis(timeout));
Future f = invoke(op);
try {
return f.get();
} catch (ExecutionException e) {
throw rethrowAllowInterrupted(e);
}
}
@Override
public void countDown() {
CountDownOperation op = new CountDownOperation(name);
InternalCompletableFuture f = invoke(op);
f.getSafely();
}
@Override
public int getCount() {
GetCountOperation op = new GetCountOperation(name);
InternalCompletableFuture f = invoke(op);
return f.getSafely();
}
@Override
public boolean trySetCount(int count) {
checkNotNegative(count, "count can't be negative");
SetCountOperation op = new SetCountOperation(name, count);
InternalCompletableFuture f = invoke(op);
return f.getSafely();
}
private InternalCompletableFuture invoke(Operation op) {
NodeEngine nodeEngine = getNodeEngine();
OperationService operationService = nodeEngine.getOperationService();
return operationService.invokeOnPartition(CountDownLatchService.SERVICE_NAME, op, partitionId);
}
@Override
public String getServiceName() {
return CountDownLatchService.SERVICE_NAME;
}
@Override
public String toString() {
return "ICountDownLatch{name='" + name + '\'' + '}';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy