001package conexp.fx.core.collections.relation;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2023 Francesco Kriegel
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025import java.util.Collections;
026import java.util.Set;
027
028import conexp.fx.core.collections.Pair;
029
030public final class RelationEvent<R, C> {
031
032  public enum Type {
033    ANY(null),
034    ROWS(ANY),
035    ROWS_ADDED(ROWS),
036    ROWS_SET(ROWS),
037    ROWS_REMOVED(ROWS),
038    ROWS_CLEARED(ROWS),
039    COLUMNS(ANY),
040    COLUMNS_ADDED(COLUMNS),
041    COLUMNS_SET(COLUMNS),
042    COLUMNS_REMOVED(COLUMNS),
043    COLUMNS_CLEARED(COLUMNS),
044    ENTRIES(ANY),
045    ENTRIES_ADDED(ENTRIES),
046    ENTRIES_REMOVED(ENTRIES),
047    ALL_CHANGED(ENTRIES),
048    SELECTION_CHANGED(ANY);
049
050    private final Type superType;
051
052    Type(final Type superType) {
053      this.superType = superType;
054    }
055
056    public final Type getSuperType() {
057      return superType;
058    }
059  }
060
061  public static final RelationEvent.Type ANY               = RelationEvent.Type.ANY;
062  public static final RelationEvent.Type ROWS              = RelationEvent.Type.ROWS;
063  public static final RelationEvent.Type ROWS_ADDED        = RelationEvent.Type.ROWS_ADDED;
064  public static final RelationEvent.Type ROWS_SET          = RelationEvent.Type.ROWS_SET;
065  public static final RelationEvent.Type ROWS_REMOVED      = RelationEvent.Type.ROWS_REMOVED;
066  public static final RelationEvent.Type ROWS_CLEARED      = RelationEvent.Type.ROWS_CLEARED;
067  public static final RelationEvent.Type COLUMNS           = RelationEvent.Type.COLUMNS;
068  public static final RelationEvent.Type COLUMNS_ADDED     = RelationEvent.Type.COLUMNS_ADDED;
069  public static final RelationEvent.Type COLUMNS_SET       = RelationEvent.Type.COLUMNS_SET;
070  public static final RelationEvent.Type COLUMNS_REMOVED   = RelationEvent.Type.COLUMNS_REMOVED;
071  public static final RelationEvent.Type COLUMNS_CLEARED   = RelationEvent.Type.COLUMNS_CLEARED;
072  public static final RelationEvent.Type ENTRIES           = RelationEvent.Type.ENTRIES;
073  public static final RelationEvent.Type ENTRIES_ADDED     = RelationEvent.Type.ENTRIES_ADDED;
074  public static final RelationEvent.Type ENTRIES_REMOVED   = RelationEvent.Type.ENTRIES_REMOVED;
075  public static final RelationEvent.Type ALL_CHANGED       = RelationEvent.Type.ALL_CHANGED;
076  public static final RelationEvent.Type SELECTION_CHANGED = RelationEvent.Type.SELECTION_CHANGED;
077
078  private final RelationEvent.Type       type;
079  private final Set<R>                   rows;
080  private final Set<Pair<R, R>>          setRows;
081  private final Set<C>                   cols;
082  private final Set<Pair<C, C>>          setCols;
083  private final Set<Pair<R, C>>          entries;
084
085  public RelationEvent(final RelationEvent.Type type) {
086    super();
087    this.type = type;
088    this.rows = Collections.unmodifiableSet(Collections.<R> emptySet());
089    this.setRows = Collections.unmodifiableSet(Collections.<Pair<R, R>> emptySet());
090    this.cols = Collections.unmodifiableSet(Collections.<C> emptySet());
091    this.setCols = Collections.unmodifiableSet(Collections.<Pair<C, C>> emptySet());
092    this.entries = Collections.unmodifiableSet(Collections.<Pair<R, C>> emptySet());
093  }
094
095  public RelationEvent(final RelationEvent.Type type, final R row, final C col) {
096    this(type, row == null ? null : Collections.singleton(row), col == null ? null : Collections.singleton(col), null);
097  }
098
099  public RelationEvent(
100      final RelationEvent.Type type,
101      final Set<R> rows,
102      final Set<C> columns,
103      final Set<Pair<R, C>> entries) {
104    super();
105    this.type = type;
106    this.rows = Collections.unmodifiableSet(rows == null ? Collections.<R> emptySet() : rows);
107    this.setRows = Collections.unmodifiableSet(Collections.<Pair<R, R>> emptySet());
108    this.cols = Collections.unmodifiableSet(columns == null ? Collections.<C> emptySet() : columns);
109    this.setCols = Collections.unmodifiableSet(Collections.<Pair<C, C>> emptySet());
110    this.entries = Collections.unmodifiableSet(entries == null ? Collections.<Pair<R, C>> emptySet() : entries);
111  }
112
113  public RelationEvent(final RelationEvent.Type type, final Pair<R, R> setRow, final Pair<C, C> setColumn) {
114    this(
115        type,
116        setRow == null ? null : Collections.singleton(setRow),
117        setColumn == null ? null : Collections.singleton(setColumn));
118  }
119
120  public RelationEvent(final RelationEvent.Type type, final Set<Pair<R, R>> setRows, final Set<Pair<C, C>> setColumns) {
121    super();
122    this.type = type;
123    this.rows = Collections.unmodifiableSet(Collections.<R> emptySet());
124    this.setRows = Collections.unmodifiableSet(setRows == null ? Collections.<Pair<R, R>> emptySet() : setRows);
125    this.cols = Collections.unmodifiableSet(Collections.<C> emptySet());
126    this.setCols = Collections.unmodifiableSet(setColumns == null ? Collections.<Pair<C, C>> emptySet() : setColumns);
127    this.entries = Collections.unmodifiableSet(Collections.<Pair<R, C>> emptySet());
128  }
129
130  public final RelationEvent.Type getType() {
131    return type;
132  }
133
134  public final Set<R> getRows() {
135    return rows;
136  }
137
138  public final Set<Pair<R, R>> getSetRows() {
139    return setRows;
140  }
141
142  public final Set<C> getColumns() {
143    return cols;
144  }
145
146  public final Set<Pair<C, C>> getSetColumns() {
147    return setCols;
148  }
149
150  public final Set<Pair<R, C>> getEntries() {
151    return entries;
152  }
153}