Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 713039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:54:59+00:00 2026-05-14T04:54:59+00:00

I need to wrap a List<T> with some class that allows calls to set/get

  • 0

I need to wrap a List<T> with some class that allows calls to set/get but does not allow add/remove calls, so that the list remains “stuck” at a fixed length. I think I have a thin wrapper class (below) that will work, but I’m not 100% positive.

Did I miss anything obvious?

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

class RestrictedListWrapper<T> implements List<T>
{
    static <T> T fail() throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException();
    }   

    static private class IteratorWrapper<T> implements ListIterator<T>
    {
        final private ListIterator<T> iter;

        private IteratorWrapper(ListIterator<T> iter) { this.iter = iter; }
        static public <T> RestrictedListWrapper.IteratorWrapper<T> wrap(ListIterator<T> target) { 
            return new RestrictedListWrapper.IteratorWrapper<T>(target); 
        }
        @Override public void add(T e) { fail(); }
        @Override public boolean hasNext() { return this.iter.hasNext(); }
        @Override public boolean hasPrevious() { return this.iter.hasPrevious(); }
        @Override public T next() { return this.iter.next(); }
        @Override public int nextIndex() { return this.iter.nextIndex(); }
        @Override public T previous() { return this.iter.previous(); }
        @Override public int previousIndex() { return this.iter.previousIndex(); }
        @Override public void remove() { fail(); }
        @Override public void set(T e) { this.iter.set(e); }
    }       

    final private List<T> list;

    private RestrictedListWrapper(List<T> list) {
        this.list = list;
    }
    static public <T> RestrictedListWrapper<T> wrap(List<T> target) {
        return new RestrictedListWrapper<T>(target);
    }
    @Override public boolean add(T arg0) { return fail();  } 
    @Override public void add(int index, T element) { fail(); }
    @Override public boolean addAll(Collection<? extends T> arg0) {
        return fail(); 
    }
    @Override public boolean addAll(int arg0, Collection<? extends T> arg1) {
        return fail();
    }

    /**
     * clear() allows setting all members of the list to null
     */
    @Override public void clear() {
        ListIterator<T> it = this.list.listIterator();

        while (it.hasNext())
        {
            it.set(null);
            it.next();
        }
    }
    @Override public boolean contains(Object o) {
        return this.list.contains(o);
    }
    @Override public boolean containsAll(Collection<?> c) {
        return this.list.containsAll(c);
    }
    @Override public T get(int index) { return this.list.get(index); }
    @Override public int indexOf(Object o) { return this.list.indexOf(o); }
    @Override public boolean isEmpty() { return false; }
    @Override public Iterator<T> iterator() { 
        return listIterator();
    }
    @Override public int lastIndexOf(Object o) { return this.list.lastIndexOf(o); }
    @Override public ListIterator<T> listIterator() {
        return IteratorWrapper.wrap(this.list.listIterator());
    }
    @Override public ListIterator<T> listIterator(int index) {
        return IteratorWrapper.wrap(this.list.listIterator(index));
    }
    @Override public boolean remove(Object o) { return fail(); }
    @Override public T remove(int index) { fail(); return fail(); }
    @Override public boolean removeAll(Collection<?> c) { return fail(); }
    @Override public boolean retainAll(Collection<?> c) { return fail(); }

    @Override public T set(int index, T element) { return this.list.set(index, element); }
    @Override public int size() { return this.list.size(); }
    @Override public List<T> subList(int fromIndex, int toIndex) {
        return new RestrictedListWrapper<T>(this.list.subList(fromIndex, toIndex));
    }
    @Override public Object[] toArray() { return this.list.toArray(); }
    @Override public <T> T[] toArray(T[] a) { return this.list.toArray(a); }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:55:00+00:00Added an answer on May 14, 2026 at 4:55 am

    Apache Commons Collections has a FixedSizedList class that does exactly that.

    Decorates another List to fix the size
    preventing add/remove.

    The add, remove, clear and retain
    operations are unsupported. The set
    method is allowed (as it doesn’t
    change the list size).

    LarvaLabs supplies a java5-generics-friendly version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To implement data access code in our application we need some framework to wrap
Need a way to allow sorting except for last item with in a list.
I need to wrap a few strings in single quotes for a dynamic TSQL
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
need ask you about some help. I have web app running in Net 2.0.
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
I need to take some simple UL tag generated in PHP (Joomla 1.5) and
I have text with URL and I need to wrap them with HTML A
Because of the complexity of this application, I have a need to wrap Facebook

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.