Home | History | Annotate | Download | only in build
      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 
     36         BuildFile(String folder, String fileName) {
     37             this.folder = new File(folder);
     38             this.fileName = new File(this.folder, fileName);
     39         }
     40 
     41         String getPath() {
     42             return fileName.getAbsolutePath();
     43         }
     44 
     45         @Override
     46         public int hashCode() {
     47             return fileName.hashCode();
     48         }
     49 
     50         @Override
     51         public boolean equals(Object obj) {
     52             if (obj == null) return false;
     53             if (this == obj) return true;
     54             if (getClass() == obj.getClass()) {
     55                 BuildFile other = (BuildFile) obj;
     56                 return fileName.equals(other.fileName);
     57             }
     58             return false;
     59         }
     60     }
     61 
     62     BuildStep(BuildFile inputFile, BuildFile outputFile) {
     63         if (inputFile == null) {
     64             throw new NullPointerException("inputFile is null");
     65         }
     66         if (outputFile == null) {
     67             throw new NullPointerException("outputFile is null");
     68         }
     69         this.inputFile = inputFile;
     70         this.outputFile = outputFile;
     71     }
     72 
     73     BuildStep() {
     74     }
     75 
     76     private Set<BuildStep> children;
     77 
     78     boolean build() {
     79         if (children != null) {
     80             for (BuildStep child : children) {
     81                 if (!child.build()) {
     82                     return false;
     83                 }
     84             }
     85         }
     86         return true;
     87     }
     88 
     89     @Override
     90     public boolean equals(Object obj) {
     91         if (obj == null) return false;
     92         if (obj == this) return true;
     93         return this.getClass() == obj.getClass();
     94     }
     95 
     96     @Override
     97     public abstract int hashCode();
     98 
     99     public void addChild(BuildStep child) {
    100         if (children == null) {
    101             children = new HashSet<BuildStep>();
    102         }
    103         children.add(child);
    104     }
    105 
    106     public static void copyFile(File in, File out) throws IOException {
    107         FileChannel inChannel = new FileInputStream(in).getChannel();
    108         FileChannel outChannel = new FileOutputStream(out).getChannel();
    109         try {
    110             inChannel.transferTo(0, inChannel.size(), outChannel);
    111         } catch (IOException e) {
    112             throw e;
    113         } finally {
    114             if (inChannel != null) inChannel.close();
    115             if (outChannel != null) outChannel.close();
    116         }
    117     }
    118 
    119     public int compareTo(BuildStep o) {
    120         return (inputFile == o.inputFile ? 0 : (inputFile != null
    121                 ? (o.inputFile != null ? inputFile.getPath().compareTo(
    122                         o.inputFile.getPath()) : 1) : -1));
    123     }
    124 }
    125