Skip to content

Commit

Permalink
+ Conditions now have toString methods.
Browse files Browse the repository at this point in the history
+ new condition: a directory contain a file that matches a regular
expression
  • Loading branch information
aharon hacmon committed Jan 26, 2014
1 parent e6e0a7f commit 241bd91
Showing 1 changed file with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public static Predicate<Hdfs> fileExists(final String path){
public static Predicate<Hdfs> fileExists(final Path path){
return new Predicate<Hdfs>() {

@Override
public String toString() {
return "the file in " + path.toString() + " to be openable";
};

@Override
public boolean apply(Hdfs hdfs) {
try {
Expand All @@ -44,14 +49,18 @@ public boolean apply(Hdfs hdfs) {
throw new RuntimeException(e);
}
return true;


}
};
}

public static HdfsExpectedCondition<InputStream> fileIsReadable(final Path path){
return new HdfsExpectedCondition<InputStream>() {
return new HdfsExpectedCondition<InputStream>() {

@Override
public String toString() {
return "the file in " + path.toString() + " to be openable";
};

@Override
public InputStream apply(Hdfs hdfs) {
try {
Expand All @@ -72,7 +81,12 @@ public InputStream apply(Hdfs hdfs) {
}

public static Predicate<Hdfs> directoryConatins(final Path directory, final String file){
return new Predicate<Hdfs> () {
return new Predicate<Hdfs> () {
@Override
public String toString() {
return "the directory " + directory.toString() + " to contain the file " + file;
};

@Override
public boolean apply(Hdfs hdfs) {
try {
Expand All @@ -99,5 +113,38 @@ public boolean apply(Hdfs hdfs) {
};
}



public static Predicate<Hdfs> directoryConatins(final Path directory, final String file, final boolean isRegexp){
if (!isRegexp) return directoryConatins(directory, file);
return new Predicate<Hdfs> () {
@Override
public String toString() {
return "the directory " + directory.toString() + " to contain a file matchin the regular expression " + file;
};

@Override
public boolean apply(Hdfs hdfs) {
try {
RemoteIterator<LocatedFileStatus> locatedStatusIterator = hdfs.listLocatedStatus(directory);
while (locatedStatusIterator.hasNext()){
LocatedFileStatus locatedFileStatus = locatedStatusIterator.next();
if (locatedFileStatus.getPath().getName().matches(file)){
return true;
}
}
} catch (AccessControlException e) {
throw new RuntimeException(e);
} catch (FileNotFoundException e) {
return false;
} catch (UnresolvedLinkException e) {
return false;
} catch (IllegalArgumentException e) {
throw e;
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;
}
};
}
}

0 comments on commit 241bd91

Please sign in to comment.