1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package util.build; 18 19 import java.io.File; 20 import java.io.FileInputStream; 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 import java.nio.channels.FileChannel; 24 import java.util.HashSet; 25 import java.util.Set; 26 27 abstract class BuildStep implements Comparable<BuildStep> { 28 29 BuildFile inputFile; 30 BuildFile outputFile; 31 32 static class BuildFile { 33 final File folder; 34 final File fileName; 35 BuildFile(String folder, String fileName)36 BuildFile(String folder, String fileName) { 37 this.folder = new File(folder); 38 this.fileName = new File(this.folder, fileName); 39 } BuildFile(File file)40 BuildFile(File file) { 41 this.folder = file.getParentFile(); 42 this.fileName = file; 43 } 44 getPath()45 String getPath() { 46 return fileName.getAbsolutePath(); 47 } 48 49 @Override hashCode()50 public int hashCode() { 51 return fileName.hashCode(); 52 } 53 54 @Override equals(Object obj)55 public boolean equals(Object obj) { 56 if (obj == null) return false; 57 if (this == obj) return true; 58 if (getClass() == obj.getClass()) { 59 BuildFile other = (BuildFile) obj; 60 return fileName.equals(other.fileName); 61 } 62 return false; 63 } 64 } 65 BuildStep(BuildFile inputFile, BuildFile outputFile)66 BuildStep(BuildFile inputFile, BuildFile outputFile) { 67 if (inputFile == null) { 68 throw new NullPointerException("inputFile is null"); 69 } 70 if (outputFile == null) { 71 throw new NullPointerException("outputFile is null"); 72 } 73 this.inputFile = inputFile; 74 this.outputFile = outputFile; 75 } 76 BuildStep(File output)77 BuildStep(File output) { 78 this.outputFile = new BuildFile(output); 79 } 80 81 private Set<BuildStep> children; 82 build()83 boolean build() { 84 if (children != null) { 85 for (BuildStep child : children) { 86 if (!child.build()) { 87 return false; 88 } 89 } 90 } 91 return true; 92 } 93 94 @Override equals(Object obj)95 public boolean equals(Object obj) { 96 if (obj == null) return false; 97 if (obj == this) return true; 98 return this.getClass() == obj.getClass(); 99 } 100 101 @Override hashCode()102 public abstract int hashCode(); 103 addChild(BuildStep child)104 public void addChild(BuildStep child) { 105 if (children == null) { 106 children = new HashSet<BuildStep>(); 107 } 108 children.add(child); 109 } 110 copyFile(File in, File out)111 public static void copyFile(File in, File out) throws IOException { 112 FileChannel inChannel = new FileInputStream(in).getChannel(); 113 FileChannel outChannel = new FileOutputStream(out).getChannel(); 114 try { 115 inChannel.transferTo(0, inChannel.size(), outChannel); 116 } catch (IOException e) { 117 throw e; 118 } finally { 119 if (inChannel != null) inChannel.close(); 120 if (outChannel != null) outChannel.close(); 121 } 122 } 123 compareTo(BuildStep o)124 public int compareTo(BuildStep o) { 125 return (outputFile == o.outputFile ? 0 : outputFile.getPath().compareTo( 126 o.outputFile.getPath())); 127 } 128 } 129