001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.index;
021    
022    import com.google.common.collect.Lists;
023    import org.sonar.api.resources.Resource;
024    
025    import javax.annotation.Nullable;
026    
027    import java.util.Collections;
028    import java.util.List;
029    
030    public final class Bucket {
031    
032      private Resource resource;
033    
034      private Bucket parent;
035      private List<Bucket> children;
036    
037      public Bucket(Resource resource) {
038        this.resource = resource;
039      }
040    
041      public Resource getResource() {
042        return resource;
043      }
044    
045      public Bucket setParent(@Nullable Bucket parent) {
046        this.parent = parent;
047        if (parent != null) {
048          parent.addChild(this);
049        }
050        return this;
051      }
052    
053      private Bucket addChild(Bucket child) {
054        if (children == null) {
055          children = Lists.newArrayList();
056        }
057        children.add(child);
058        return this;
059      }
060    
061      private void removeChild(Bucket child) {
062        if (children != null) {
063          children.remove(child);
064        }
065      }
066    
067      public List<Bucket> getChildren() {
068        return children == null ? Collections.<Bucket>emptyList() : children;
069      }
070    
071      public Bucket getParent() {
072        return parent;
073      }
074    
075      public void clear() {
076        children = null;
077        if (parent != null) {
078          parent.removeChild(this);
079          parent = null;
080        }
081      }
082    
083      @Override
084      public boolean equals(Object o) {
085        if (this == o) {
086          return true;
087        }
088        if (o == null || getClass() != o.getClass()) {
089          return false;
090        }
091        Bucket that = (Bucket) o;
092        return resource.equals(that.resource);
093      }
094    
095      @Override
096      public int hashCode() {
097        return resource.hashCode();
098      }
099    }