extended File API

This commit is contained in:
Timo Westkämper 2010-03-26 15:11:16 +00:00
parent c73353c3d4
commit 09a2ccbeda
31 changed files with 244 additions and 40 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query;
import static org.junit.Assert.assertEquals;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query;
import java.io.IOException;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.alias;
import com.mysema.query.types.Expr;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.alias;
import static com.mysema.query.alias.Alias.$;

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.alias;
import java.util.ArrayList;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.animal;
import static org.junit.Assert.assertEquals;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.animal;
import static org.junit.Assert.assertTrue;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.animal;
import static org.junit.Assert.assertEquals;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.collections;
import org.junit.Test;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.file;
import java.io.File;
import java.util.Iterator;
/**
* @author tiwe
*
*/
public class DirectoryWalk implements Iterable<File>{
private final File directory;
public DirectoryWalk(String path) {
this.directory = new File(path);
}
public DirectoryWalk(File directory) {
this.directory = directory;
}
@Override
public Iterator<File> iterator() {
return new DirectoryWalkIterator(directory);
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.file;
import java.io.File;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* @author tiwe
*
*/
public class DirectoryWalkIterator implements Iterator<File> {
private final Deque<File> files = new ArrayDeque<File>();
public DirectoryWalkIterator(File directory) {
File[] children = directory.listFiles();
if (children != null){
for (File file : children){
files.add(file);
}
}
}
@Override
public boolean hasNext() {
return !files.isEmpty();
}
@Override
public File next() {
if (!files.isEmpty()){
File file = files.pop();
File[] children = file.listFiles();
if (children != null){
for (File child : children){
files.add(child);
}
}
return file;
}else{
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}

View File

@ -0,0 +1,22 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.file;
import java.io.File;
import org.junit.Test;
public class DirectoryWalkTest {
@Test
public void test(){
DirectoryWalk walk = new DirectoryWalk(new File("target"));
for (File file : walk){
System.out.println(file.getPath());
}
}
}

View File

@ -1,22 +0,0 @@
package com.mysema.query.file;
import java.io.File;
import java.util.Map;
import org.junit.Test;
import com.mysema.query.collections.MiniApi;
public class FileTest {
@Test
public void path_to_file_map(){
Map<String,File> files = MiniApi
.from(QFile.any, new File(".").listFiles())
.map(QFile.any.absolutePath, QFile.any);
for (Map.Entry<String, File> entry : files.entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.file;
import static com.mysema.query.types.path.PathMetadataFactory.forProperty;
@ -6,6 +11,9 @@ import static com.mysema.query.types.path.PathMetadataFactory.forVariable;
import java.io.File;
import com.mysema.query.types.PathMetadata;
import com.mysema.query.types.custom.CString;
import com.mysema.query.types.expr.EString;
import com.mysema.query.types.expr.EStringConst;
import com.mysema.query.types.path.PBoolean;
import com.mysema.query.types.path.PComparable;
import com.mysema.query.types.path.PString;
@ -16,10 +24,16 @@ import com.mysema.query.types.path.PString;
*/
public class QFile extends PComparable<File>{
private static final long serialVersionUID = -7703329992523284173L;
private static final String GET_CONTENT = "org.apache.commons.io.FileUtils.readFileToString({0}, {1})";
public static final QFile any = new QFile("any");
private static final long serialVersionUID = -7703329992523284173L;
public static Iterable<File> walk(File dir){
return new DirectoryWalk(dir);
}
public final PBoolean absolute = new PBoolean(this, "absolute");
private volatile QFile absoluteFile, canonicalFile, parentFile;
@ -66,6 +80,10 @@ public class QFile extends PComparable<File>{
return canonicalFile;
}
public EString getContent(String encoding){
return CString.create(GET_CONTENT, this, EStringConst.create(encoding));
}
public QFile parentFile() {
if (parentFile == null){
parentFile = new QFile(this, "parentFile");

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.file;
import java.io.File;
import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import com.mysema.query.collections.MiniApi;
public class QFileTest {
@Test
public void path_to_file_map(){
QFile anyFile = QFile.any;
Map<String,File> files = MiniApi
.from(anyFile, new File(".").listFiles())
.map(anyFile.absolutePath, anyFile);
for (Map.Entry<String, File> entry : files.entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
@Test
public void walk(){
QFile anyFile = QFile.any;
for (File file : MiniApi
.from(anyFile, QFile.walk(new File("target")))
.where(anyFile.name.endsWith(".class"))
.list(anyFile)){
System.out.println(file.getName());
}
}
@Test
@Ignore
public void getContent(){
// FIXME
QFile anyFile = QFile.any;
Map<File,String> rv = MiniApi
.from(anyFile, QFile.walk(new File("src/test")))
.where(anyFile.name.endsWith(".properties"))
.map(anyFile, anyFile.getContent("ISO-8859-1"));
System.out.println(rv);
}
}