Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1ec61d8aee8b447b27886e965aaa41eb259a652d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (c) 2012, 2015, 2016, 2019, 2022 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.container;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;

/**
 * An implementation of a {@link Container container} that stores its {@link #getElements() elements} in a {@link #getSet() set}.
 *
 * @since 3.2
 * @author Eike Stepper
 */
public class SetContainer<E> extends PersistableContainer<E>
{
  private final Set<E> set;

  public SetContainer(Class<E> componentType)
  {
    this(componentType, new HashSet<E>());
  }

  public SetContainer(Class<E> componentType, Set<E> set)
  {
    super(componentType);
    this.set = set;
  }

  protected Set<E> getSet()
  {
    return set;
  }

  @Override
  protected boolean backingStoreIsEmpty()
  {
    return set.isEmpty();
  }

  @Override
  protected int backingStoreSize()
  {
    return set.size();
  }

  @Override
  protected E[] backingStoreToArray(E[] a)
  {
    return set.toArray(a);
  }

  @Override
  protected void backingStoreForEach(Consumer<E> consumer)
  {
    if (consumer != null)
    {
      for (E element : set)
      {
        consumer.accept(element);
      }
    }
  }

  @Override
  protected boolean backingStoreContains(E element)
  {
    return set.contains(element);
  }

  @Override
  protected boolean backingStoreAdd(E element)
  {
    return set.add(element);
  }

  @Override
  protected boolean backingStoreRemove(E element)
  {
    return set.remove(element);
  }

  @Override
  protected void backingStoreClear()
  {
    set.clear();
  }
}

Back to the top