1 /*
2 * Copyright (c) 2011-2024 Qulice.com
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met: 1) Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer. 2) Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution. 3) Neither the name of the Qulice.com nor
14 * the names of its contributors may be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 package com.qulice.ant;
32
33 import java.io.File;
34 import java.util.function.Function;
35 import java.util.function.Supplier;
36 import org.apache.tools.ant.Project;
37
38 /**
39 * Represents subset of org.apache.tools.ant.Project API which is relevant to Qulice.
40 *
41 * @since 1.0
42 */
43 interface AntProject {
44 /**
45 * Returns Ant project property.
46 *
47 * @param property Name of the property to get.
48 * @return Property value or null.
49 */
50 String getProperty(String property);
51
52 /**
53 * Returns Ant project base directory.
54 *
55 * @return Base directory.
56 */
57 File getBaseDir();
58
59 /**
60 * Default implementation which wraps Ant Project.
61 * @since 1.0
62 */
63 class Default implements AntProject {
64 /**
65 * Wrapped project.
66 */
67 private final Project project;
68
69 /**
70 * Creates a new AntProject equivalent to the given Project.
71 * @param project Project to wrap.
72 */
73 Default(final Project project) {
74 this.project = project;
75 }
76
77 @Override
78 public String getProperty(final String property) {
79 return this.project.getProperty(property);
80 }
81
82 @Override
83 public File getBaseDir() {
84 return this.project.getBaseDir();
85 }
86 }
87
88 /**
89 * Fake implementation of AntProject which allows caller to
90 * customize both methods with lambdas.
91 *
92 * @since 1.0
93 */
94 class Fake implements AntProject {
95 /**
96 * Implementation of the getProperty() method.
97 */
98 private final Function<String, String> prop;
99
100 /**
101 * Implementation of the getBaseDir() method.
102 */
103 private final Supplier<File> basedir;
104
105 /**
106 * Creates a new FakeAntProject which will use provided
107 * functions.
108 *
109 * @param prop Implementation of the getProperty(String).
110 * @param basedir Implementation of the getBaseDir().
111 */
112 Fake(
113 final Function<String, String> prop,
114 final Supplier<File> basedir
115 ) {
116 this.prop = prop;
117 this.basedir = basedir;
118 }
119
120 @Override
121 public String getProperty(final String property) {
122 return this.prop.apply(property);
123 }
124
125 @Override
126 public File getBaseDir() {
127 return this.basedir.get();
128 }
129 }
130 }