<commit_before>/*
 * Copyright 2000-2014 Vaadin Ltd.
 *
 * 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.vaadin.client.widget.grid.datasources;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.vaadin.client.data.DataChangeHandler;
import com.vaadin.client.data.DataSource;
import com.vaadin.client.widget.grid.events.SelectAllEvent;
import com.vaadin.client.widget.grid.events.SelectAllHandler;
import com.vaadin.shared.util.SharedUtil;

/**
 * A simple list based on an in-memory data source for simply adding a list of
 * row pojos to the grid. Based on a wrapped list instance which supports adding
 * and removing of items.
 * 
 * <p>
 * Usage:
 * 
 * <pre>
 * ListDataSource&lt;Integer&gt; ds = new ListDataSource&lt;Integer&gt;(1, 2, 3, 4);
 * 
 * // Add item to the data source
 * ds.asList().add(5);
 * 
 * // Remove item from the data source
 * ds.asList().remove(3);
 * 
 * // Add multiple items
 * ds.asList().addAll(Arrays.asList(5, 6, 7));
 * </pre>
 * 
 * @since 7.4
 * @author <NAME>
 */
public class ListDataSource<T> implements DataSource<T> {

    private class RowHandleImpl extends RowHandle<T> {

        private final T row;

        public RowHandleImpl(T row) {
            this.row = row;
        }

        @Override
        public T getRow() {
            /*
             * We'll cheat here and don't throw an IllegalStateException even if
             * this isn't pinned, because we know that the reference never gets
             * stale.
             */
            return row;
        }

        @Override
        public void pin() {
            // NOOP, really
        }

        @Override
        public void unpin() throws IllegalStateException {
            /*
             * Just to make things easier for everyone, we won't throw the
             * exception, even in illegal situations.
             */
        }

        @Override
        protected boolean equalsExplicit(Object obj) {
            if (obj instanceof ListDataSource.RowHandleImpl) {
                /*
                 * Java prefers AbstractRemoteDataSource<?>.RowHandleImpl. I
                 * like the @SuppressWarnings more (keeps the line length in
                 * check.)
                 */
                @SuppressWarnings("unchecked")
                RowHandleImpl rhi = (RowHandleImpl) obj;
                return SharedUtil.equals(row, rhi.row);
            } else {
                return false;
            }
        }

        @Override
        protected int hashCodeExplicit() {
            return row.hashCode();
        }

        @Override
        public void updateRow() {
            if (changeHandler != null) {
                changeHandler.dataUpdated(ds.indexOf(getRow()), 1);
            }
        }
    }

    /**
     * Wraps the datasource list and notifies the change handler of changing to
     * the list
     */
    private class ListWrapper implements List<T> {

        @Override
        public int size() {
            return ds.size();
        }

        @Override
        public boolean isEmpty() {
            return ds.isEmpty();
        }

        @Override
        public boolean contains(Object o) {
            return contains(o);
        }

        @Override
        public Iterator<T> iterator() {
            return new ListWrapperIterator(ds.iterator());
        }

        @Override
        public Object[] toArray() {
            return ds.toArray();
        }

        @Override
        @SuppressWarnings("hiding")
        public <T> T[] toArray(T[] a) {
            return toArray(a);
        }

        @Override
        public boolean add(T e) {
            if (ds.add(e)) {
                if (changeHandler != null) {
                    changeHandler.dataAdded(ds.size() - 1, 1);
                }
                return true;<commit_msg>Fix stack overflows in ListDataSource ListWrapper (#19104)<commit_after>/*
 * Copyright 2000-2014 Vaadin Ltd.
 *
 * 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.vaadin.client.widget.grid.datasources;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.vaadin.client.data.DataChangeHandler;
import com.vaadin.client.data.DataSource;
import com.vaadin.client.widget.grid.events.SelectAllEvent;
import com.vaadin.client.widget.grid.events.SelectAllHandler;
import com.vaadin.shared.util.SharedUtil;

/**
 * A simple list based on an in-memory data source for simply adding a list of
 * row pojos to the grid. Based on a wrapped list instance which supports adding
 * and removing of items.
 * 
 * <p>
 * Usage:
 * 
 * <pre>
 * ListDataSource&lt;Integer&gt; ds = new ListDataSource&lt;Integer&gt;(1, 2, 3, 4);
 * 
 * // Add item to the data source
 * ds.asList().add(5);
 * 
 * // Remove item from the data source
 * ds.asList().remove(3);
 * 
 * // Add multiple items
 * ds.asList().addAll(Arrays.asList(5, 6, 7));
 * </pre>
 * 
 * @since 7.4
 * @author Vaadin Ltd
 */
public class ListDataSource<T> implements DataSource<T> {

    private class RowHandleImpl extends RowHandle<T> {

        private final T row;

        public RowHandleImpl(T row) {
            this.row = row;
        }

        @Override
        public T getRow() {
            /*
             * We'll cheat here and don't throw an IllegalStateException even if
             * this isn't pinned, because we know that the reference never gets
             * stale.
             */
            return row;
        }

        @Override
        public void pin() {
            // NOOP, really
        }

        @Override
        public void unpin() throws IllegalStateException {
            /*
             * Just to make things easier for everyone, we won't throw the
             * exception, even in illegal situations.
             */
        }

        @Override
        protected boolean equalsExplicit(Object obj) {
            if (obj instanceof ListDataSource.RowHandleImpl) {
                /*
                 * Java prefers AbstractRemoteDataSource<?>.RowHandleImpl. I
                 * like the @SuppressWarnings more (keeps the line length in
                 * check.)
                 */
                @SuppressWarnings("unchecked")
                RowHandleImpl rhi = (RowHandleImpl) obj;
                return SharedUtil.equals(row, rhi.row);
            } else {
                return false;
            }
        }

        @Override
        protected int hashCodeExplicit() {
            return row.hashCode();
        }

        @Override
        public void updateRow() {
            if (changeHandler != null) {
                changeHandler.dataUpdated(ds.indexOf(getRow()), 1);
            }
        }
    }

    /**
     * Wraps the datasource list and notifies the change handler of changing to
     * the list
     */
    private class ListWrapper implements List<T> {

        @Override
        public int size() {
            return ds.size();
        }

        @Override
        public boolean isEmpty() {
            return ds.isEmpty();
        }

        @Override
        public boolean contains(Object o) {
            return ds.contains(o);
        }

        @Override
        public Iterator<T> iterator() {
            return new ListWrapperIterator(ds.iterator());
        }

        @Override
        public Object[] toArray() {
            return ds.toArray();
        }

        @Override
        @SuppressWarnings("hiding")
        public <T> T[] toArray(T[] a) {
            return ds.toArray(a);
        }

        @Override
        public boolean add(T e) {
            if (ds.add(e)) {
                if (changeHandler != null) {
                    changeHandler.dataAdded(ds.size() - 1, 1);
                }
                return true;
{'fraction_non_alphanumeric': 0.09553719008264462, 'fraction_numerical': 0.00628099173553719, 'mean_word_length': 1.8941326530612246, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4155572', 'n_tokens_mistral': 2489, 'n_tokens_neox': 2360, 'n_words': 762}