com.jivesoftware.os.hwal.permit.ExpirablePermit.java Source code

Java tutorial

Introduction

Here is the source code for com.jivesoftware.os.hwal.permit.ExpirablePermit.java

Source

/*
 * Copyright 2014 Jive Software.
 *
 * 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.jivesoftware.os.hwal.permit;

import com.google.common.base.Optional;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

/**
 *
 * @author jonathan
 */
public class ExpirablePermit {

    private final AtomicReference<Permit> expirableDelegatePermit;

    public ExpirablePermit(Permit permit) {
        this.expirableDelegatePermit = new AtomicReference<>(permit);
    }

    public Optional<Integer> getPermit() {
        Permit permit = expirableDelegatePermit.get();
        if (permit == null) {
            return Optional.absent();
        } else {
            return Optional.of(permit.id);
        }
    }

    public void expire() {
        expirableDelegatePermit.set(null);
    }

    @Override
    public String toString() {
        return "ExpirablePermit{" + "expirableDelegatePermit=" + expirableDelegatePermit + '}';
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + Objects.hashCode(this.expirableDelegatePermit);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ExpirablePermit other = (ExpirablePermit) obj;
        if (!Objects.equals(this.expirableDelegatePermit, other.expirableDelegatePermit)) {
            return false;
        }
        return true;
    }

}