All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jparams.store.index.SynchronizedIndex Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
package com.jparams.store.index;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class SynchronizedIndex implements Index
{
    private final Index index;
    private final Object mutex;

    public SynchronizedIndex(final Index index, final Object mutex)
    {
        this.index = index;
        this.mutex = mutex;
    }

    @Override
    public T getFirst(final Object key)
    {
        synchronized (mutex)
        {
            return index.getFirst(key);
        }
    }

    @Override
    public Optional findFirst(final Object key)
    {
        synchronized (mutex)
        {
            return index.findFirst(key);
        }
    }

    @Override
    public List get(final Object key)
    {
        synchronized (mutex)
        {
            return index.get(key);
        }
    }

    @Override
    public String getName()
    {
        return index.getName();
    }

    public Index getIndex()
    {
        return index;
    }

    @Override
    public boolean equals(final Object other)
    {
        if (this == other)
        {
            return true;
        }

        if (other == null || getClass() != other.getClass())
        {
            return false;
        }

        final SynchronizedIndex that = (SynchronizedIndex) other;
        return Objects.equals(index, that.index)
            && Objects.equals(mutex, that.mutex);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(index, mutex);
    }

    @Override
    public String toString()
    {
        return String.valueOf(index);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy