org.netbeans.modules.subversion.util.Context Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.netbeans.modules.subversion.util;
import org.netbeans.modules.versioning.spi.VersioningSupport;
import java.io.File;
import java.io.Serializable;
import java.util.*;
import org.netbeans.modules.versioning.util.Utils;
/**
* Encapsulates context of an action. There are two ways in which context may be defined:
* - list of files (f/b.txt, f/c.txt, f/e.txt)
* - list of roots (top folders) plus list of exclusions (f),(a.txt, d.txt)
*
* @author Maros Sandor
*/
public class Context implements Serializable {
public static final Context Empty = new Context( getEmptyList(), getEmptyList(), getEmptyList() );
private static final long serialVersionUID = 1L;
private final List filteredFiles;
private final List rootFiles;
private final List exclusions;
public Context(List filteredFiles, List rootFiles, List exclusions) {
this.filteredFiles = filteredFiles;
this.rootFiles = rootFiles;
this.exclusions = exclusions;
while (normalize());
}
public Context(File file) {
this(new File [] { file });
}
public Context(File [] files) {
List list = new ArrayList(files.length);
list.addAll(Arrays.asList(files));
removeDuplicates(list);
this.filteredFiles = list;
this.rootFiles = list;
this.exclusions = Collections.emptyList();
}
private boolean normalize() {
for (Iterator i = rootFiles.iterator(); i.hasNext();) {
File root = i.next();
for (Iterator j = exclusions.iterator(); j.hasNext();) {
File exclusion = j.next();
if (Utils.isAncestorOrEqual(exclusion, root)) {
j.remove();
exclusionRemoved(exclusion, root);
return true;
}
}
}
removeDuplicates(rootFiles);
removeDuplicates(exclusions);
return false;
}
private void removeDuplicates(List files) {
List newFiles = new ArrayList();
outter: for (Iterator i = files.iterator(); i.hasNext();) {
File file = i.next();
for (Iterator j = newFiles.iterator(); j.hasNext();) {
File includedFile = j.next();
if (Utils.isAncestorOrEqual(includedFile, file) && (file.isFile() || !VersioningSupport.isFlat(includedFile))) continue outter;
if (Utils.isAncestorOrEqual(file, includedFile) && (includedFile.isFile() || !VersioningSupport.isFlat(file))) {
j.remove();
}
}
newFiles.add(file);
}
files.clear();
files.addAll(newFiles);
}
private void exclusionRemoved(File exclusion, File root) {
File [] exclusionChildren = exclusion.listFiles();
if (exclusionChildren == null) return;
for (int i = 0; i < exclusionChildren.length; i++) {
File child = exclusionChildren[i];
if (!Utils.isAncestorOrEqual(root, child)) {
exclusions.add(child);
}
}
}
public List getRoots() {
return rootFiles;
}
public List getExclusions() {
return exclusions;
}
/**
* Gets exact set of files to operate on, it is effectively defined as (rootFiles - exclusions). This set
* is NOT suitable for Update command because Update should operate on all rootFiles and just exclude some subfolders.
* Otherwise update misses new files and folders directly in rootFiles folders.
*
* @return files to operate on
*/
public File [] getFiles() {
return filteredFiles.toArray(new File[0]);
}
public File[] getRootFiles() {
return rootFiles.toArray(new File[0]);
}
public boolean contains(File file) {
outter : for (Iterator i = rootFiles.iterator(); i.hasNext();) {
File root = (File) i.next();
if (SvnUtils.isParentOrEqual(root, file)) {
for (Iterator j = exclusions.iterator(); j.hasNext();) {
File excluded = (File) j.next();
if (Utils.isAncestorOrEqual(excluded, file)) {
continue outter;
}
}
return true;
}
}
return false;
}
public static final List getEmptyList() {
return Collections.emptyList();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy