/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the “License”). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.collections;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang.Validate;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.concurrent.ThreadSafe;
/**
 * Prefix list is used to do file filtering.
 */
@ThreadSafe
public final class PrefixList {
  private final List<String> mInnerList;

  /**
   * Prefix list is used to do file filtering.
   *
   * @param prefixList the list of prefixes to create
   */
  public PrefixList(List<String> prefixList) {
    if (prefixList == null) {
      mInnerList = new ArrayList<String>(0);
    } else {
      mInnerList = prefixList;
    }
  }

  /**
   * Prefix list is used to do file filtering.
   *
   * @param prefixes the prefixes with separators
   * @param separator the separator to split the prefixes
   */
  public PrefixList(String prefixes, String separator) {
    Validate.notNull(separator);
    mInnerList = new ArrayList<String>(0);
    if (prefixes != null && !prefixes.trim().isEmpty()) {
      String[] candidates = prefixes.trim().split(separator);
      for (String prefix : candidates) {
        String trimmed = prefix.trim();
        if (!trimmed.isEmpty()) {
          mInnerList.add(trimmed);
        }
      }
    }
  }

  /**
   * Gets the list of prefixes.
   *
   * @return the list of prefixes
   */
  public List<String> getList() {
    return ImmutableList.copyOf(mInnerList);
  }

  /**
   * Checks whether a prefix of {@code path} is in the prefix list.
   *
   * @param path the path to check
   * @return true if the path is in the list, false otherwise
   */
  public boolean inList(String path) {
    if (Strings.isNullOrEmpty(path)) {
      return false;
    }

    for (String prefix : mInnerList) {
      if (path.startsWith(prefix)) {
        return true;
      }
    }

    return false;
  }

  /**
   * Checks whether a prefix of {@code path} is not in the prefix list.
   *
   * @param path the path to check
   * @return true if the path is not in the list, false otherwise
   */
  public boolean outList(String path) {
    return !inList(path);
  }

  /**
   * Print out all prefixes separated by ";".
   *
   * @return the string representation like "a;b/c"
   */
  @Override
  public String toString() {
    StringBuilder s = new StringBuilder();
    for (String prefix : mInnerList) {
      s.append(prefix).append(";");
    }
    return s.toString();
  }
}

@@ -878,16 +878,25 @@
 e final 
+Immutable
 List%3CStr
@@ -1127,39 +1127,33 @@
 rList = 
-new ArrayList%3CString%3E(0
+ImmutableList.of(
 );%0A    %7D
@@ -1179,16 +1179,37 @@
 rList = 
+ImmutableList.copyOf(
 prefixLi
@@ -1210,16 +1210,17 @@
 efixList
+)
 ;%0A    %7D%0A
@@ -1486,30 +1486,43 @@
 rator);%0A    
-mInner
+List%3CString%3E prefix
 List = new A
@@ -1783,30 +1783,30 @@
 %7B%0A          
-mInner
+prefix
 List.add(tri
@@ -1832,24 +1832,75 @@
     %7D%0A    %7D%0A
+    mInnerList = ImmutableList.copyOf(prefixList);%0A
   %7D%0A%0A  /**%0A 
@@ -2020,37 +2020,16 @@
  return 
-ImmutableList.copyOf(
 mInnerLi
@@ -2026,25 +2026,24 @@
 n mInnerList
-)
 ;%0A  %7D%0A%0A  /**

Use ImmutableList for PrefixList