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

javolution.util.internal.table.SharedTableImpl Maven / Gradle / Ivy

The newest version!
/*
 * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
 * Copyright (C) 2012 - Javolution (http://javolution.org/)
 * All rights reserved.
 * 
 * Permission to use, copy, modify, and distribute this software is
 * freely granted, provided that this notice is preserved.
 */
package javolution.util.internal.table;

import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;

import javolution.util.internal.collection.SharedCollectionImpl;
import javolution.util.service.CollectionService;
import javolution.util.service.TableService;

/**
 * A shared view over a table allowing concurrent access and sequential updates.
 */
public class SharedTableImpl extends SharedCollectionImpl implements
        TableService {

    private static final long serialVersionUID = 0x600L; // Version.

    public SharedTableImpl(TableService target) {
        super(target);
    }

    @Override
    public void add(int index, E element) {
        lock.writeLock.lock();
        try {
            target().add(index, element);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public boolean addAll(int index, Collection c) {
        lock.writeLock.lock();
        try {
            return target().addAll(index, c);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public void addFirst(E element) {
        lock.writeLock.lock();
        try {
            target().addFirst(element);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public void addLast(E element) {
        lock.writeLock.lock();
        try {
            target().addLast(element);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public Iterator descendingIterator() {
        return new ReversedTableImpl(this).iterator(); // View on this.
    }

    @Override
    public E element() {
        return getFirst();
    }

    @Override
    public E get(int index) {
        lock.readLock.lock();
        try {
            return target().get(index);
        } finally {
            lock.readLock.unlock();
        }
    }

    @Override
    public E getFirst() {
        lock.readLock.lock();
        try {
            return target().getFirst();
        } finally {
            lock.readLock.unlock();
        }
    }

    @Override
    public E getLast() {
        lock.readLock.lock();
        try {
            return target().getLast();
        } finally {
            lock.readLock.unlock();
        }
    }

    @Override
    public int indexOf(Object element) {
        lock.readLock.lock();
        try {
            return target().indexOf(element);
        } finally {
            lock.readLock.unlock();
        }
    }

    @Override
    public ListIterator iterator() {
        return target().listIterator(0);
    }

    @Override
    public int lastIndexOf(Object element) {
        lock.readLock.lock();
        try {
            return target().lastIndexOf(element);
        } finally {
            lock.readLock.unlock();
        }
    }

    @Override
    public ListIterator listIterator() {
        return target().listIterator(0);
    }

    @Override
    public ListIterator listIterator(int index) {
        return new TableIteratorImpl(this, index); // View on this.
    }

    @Override
    public boolean offer(E e) {
        return offerLast(e);
    }

    @Override
    public boolean offerFirst(E e) {
        lock.writeLock.lock();
        try {
            return target().offerFirst(e);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public boolean offerLast(E e) {
        lock.writeLock.lock();
        try {
            return target().offerLast(e);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public E peek() {
        return peekFirst();
    }

    @Override
    public E peekFirst() {
        lock.readLock.lock();
        try {
            return target().peekFirst();
        } finally {
            lock.readLock.unlock();
        }
    }

    @Override
    public E peekLast() {
        lock.readLock.lock();
        try {
            return target().peekLast();
        } finally {
            lock.readLock.unlock();
        }
    }

    @Override
    public E poll() {
        return pollFirst();
    }

    @Override
    public E pollFirst() {
        lock.writeLock.lock();
        try {
            return target().pollFirst();
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public E pollLast() {
        lock.writeLock.lock();
        try {
            return target().pollLast();
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public E pop() {
        return removeFirst();
    }

    @Override
    public void push(E e) {
        addFirst(e);
    }

    @Override
    public E remove() {
        return removeFirst();
    }

    @Override
    public E remove(int index) {
        lock.writeLock.lock();
        try {
            return target().remove(index);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public E removeFirst() {
        lock.writeLock.lock();
        try {
            return target().removeFirst();
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public boolean removeFirstOccurrence(Object o) {
        lock.writeLock.lock();
        try {
            return target().removeFirstOccurrence(o);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public E removeLast() {
        lock.writeLock.lock();
        try {
            return target().removeLast();
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public boolean removeLastOccurrence(Object o) {
        lock.writeLock.lock();
        try {
            return target().removeLastOccurrence(o);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public E set(int index, E element) {
        lock.writeLock.lock();
        try {
            return target().set(index, element);
        } finally {
            lock.writeLock.unlock();
        }
    }

    @Override
    public CollectionService[] split(int n) {
        return SubTableImpl.splitOf(this, n); // Sub-views over this.
    }

    @Override
    public TableService subList(int fromIndex, int toIndex) {
        return new SubTableImpl(this, fromIndex, toIndex); // View on this.
    }

    @Override
    public TableService threadSafe() {
        return this;
    }

    /** Returns the actual target */
    @Override
    protected TableService target() {
        return (TableService) super.target();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy