summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/editor/structure/property/PropertyListIntersector.java
blob: 24aea2f5d67cf41bd9c320d04c01438d85d4d6e3 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * 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:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.editor.structure.property;

import com.google.common.collect.Lists;

import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.model.property.PropertyManager;

import java.util.Iterator;
import java.util.List;

/**
 * Helper for computing intersection of {@link Property} arrays.
 * 
 * @author scheglov_ke
 * @coverage core.editor.structure
 */
public final class PropertyListIntersector {
  private List<PropertyGroup> m_intersection;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Updates intersection by intersecting with new given array.
   */
  public void intersect(Property[] properties) {
    if (m_intersection == null) {
      m_intersection = Lists.newArrayList();
      for (int i = 0; i < properties.length; i++) {
        Property property = properties[i];
        m_intersection.add(new PropertyGroup(property));
      }
    } else {
      for (Iterator<PropertyGroup> I = m_intersection.iterator(); I.hasNext();) {
        PropertyGroup propertyGroup = I.next();
        if (!propertyGroup.add(properties)) {
          I.remove();
        }
      }
    }
  }

  /**
   * @return the array of matched composite {@link Property}'s.
   */
  public Property[] getProperties() {
    List<Property> properties = Lists.newArrayList();
    for (PropertyGroup propertyGroup : m_intersection) {
      Property compositeProperty = propertyGroup.getCompositeProperty();
      if (compositeProperty != null) {
        properties.add(compositeProperty);
      }
    }
    //
    return properties.toArray(new Property[properties.size()]);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // PropertyGroup
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * The group of {@link Property}'s that match.
   */
  private static final class PropertyGroup {
    private final List<Property> m_properties = Lists.newArrayList();

    ////////////////////////////////////////////////////////////////////////////
    //
    // Constructor
    //
    ////////////////////////////////////////////////////////////////////////////
    public PropertyGroup(Property property) {
      m_properties.add(property);
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Access
    //
    ////////////////////////////////////////////////////////////////////////////
    /**
     * @return <code>true</code> if new matched {@link Property} from given array was added.
     */
    public boolean add(Property[] properties) {
      for (Property property : properties) {
        if (add(property)) {
          return true;
        }
      }
      // no match
      return false;
    }

    /**
     * @return the composite {@link Property} for this group.
     */
    public Property getCompositeProperty() {
      Property properties[] = m_properties.toArray(new Property[m_properties.size()]);
      return properties[0].getComposite(properties);
    }

    ////////////////////////////////////////////////////////////////////////////
    //
    // Internal
    //
    ////////////////////////////////////////////////////////////////////////////
    /**
     * @return <code>true</code> if given {@link Property} matches and was added.
     */
    private boolean add(Property property) {
      Property example = m_properties.get(0);
      if (example.getClass() == property.getClass()
          && example.getTitle().equals(property.getTitle())
          && PropertyManager.getCategory(example) == PropertyManager.getCategory(property)) {
        m_properties.add(property);
        return true;
      }
      // no match
      return false;
    }
  }
}