001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.kernel.impl.lock;
007
008import org.fcrepo.kernel.api.identifiers.FedoraId;
009import org.fcrepo.kernel.api.lock.ResourceLock;
010import org.fcrepo.kernel.api.lock.ResourceLockType;
011
012import java.util.Objects;
013
014/**
015 * Simple implementation of the complex lock.
016 * @author whikloj
017 */
018public class ResourceLockImpl implements ResourceLock {
019
020    private final String transactionId;
021    private final ResourceLockType resourceLockType;
022    private final FedoraId resourceId;
023
024    ResourceLockImpl(final ResourceLockType resourceLock, final String txId, final FedoraId id) {
025        resourceLockType = resourceLock;
026        transactionId = txId;
027        resourceId = id;
028    }
029
030    ResourceLockImpl(final String lockType, final String txId, final FedoraId resourceId) {
031        this(ResourceLockType.fromString(lockType), txId, resourceId);
032    }
033
034    @Override
035    public FedoraId getResourceId() {
036        return resourceId;
037    }
038
039    @Override
040    public boolean hasResource(final FedoraId resourceId) {
041        return this.resourceId.equals(resourceId);
042    }
043
044    @Override
045    public ResourceLockType getLockType() {
046        return resourceLockType;
047    }
048
049    @Override
050    public boolean hasLockType(final ResourceLockType lockType) {
051        return resourceLockType.equals(lockType);
052    }
053
054    @Override
055    public boolean isAdequate(final ResourceLockType lockType) {
056        return this.resourceLockType == ResourceLockType.EXCLUSIVE || lockType == ResourceLockType.NONEXCLUSIVE;
057    }
058
059    @Override
060    public String getTransactionId() {
061        return transactionId;
062    }
063
064    @Override
065    public String toString() {
066        return String.format("type: %s, txId: %s, resource: %s", resourceLockType, transactionId,
067                resourceId);
068    }
069
070    @Override
071    public boolean equals(final Object o) {
072        if (this == o) {
073            return true;
074        }
075        if (o == null || getClass() != o.getClass()) {
076            return false;
077        }
078        final ResourceLockImpl that = (ResourceLockImpl) o;
079        return transactionId.equals(that.transactionId) && resourceId.equals(that.resourceId);
080    }
081
082    @Override
083    public int hashCode() {
084        return Objects.hash(transactionId, resourceId);
085    }
086}