• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 com.android.tools.build.apkzlib.zip;
18 
19 import com.google.common.base.Preconditions;
20 import javax.annotation.Nonnull;
21 
22 /**
23  * Factory for instances of {@link AlignmentRule}.
24  */
25 public final class AlignmentRules {
26 
AlignmentRules()27     private AlignmentRules() {}
28 
29     /**
30      * A rule that defines a constant alignment for all files.
31      *
32      * @param alignment the alignment
33      * @return the rule
34      */
constant(int alignment)35     public static AlignmentRule constant(int alignment) {
36         Preconditions.checkArgument(alignment > 0, "alignment <= 0");
37 
38         return (String path) -> alignment;
39     }
40 
41     /**
42      * A rule that defines constant alignment for all files with a certain suffix, placing no
43      * restrictions on other files.
44      *
45      * @param suffix the suffix
46      * @param alignment the alignment for paths that match the provided suffix
47      * @return the rule
48      */
constantForSuffix(@onnull String suffix, int alignment)49     public static AlignmentRule constantForSuffix(@Nonnull String suffix, int alignment) {
50         Preconditions.checkArgument(!suffix.isEmpty(), "suffix.isEmpty()");
51         Preconditions.checkArgument(alignment > 0, "alignment <= 0");
52 
53         return (String path) -> path.endsWith(suffix) ? alignment : AlignmentRule.NO_ALIGNMENT;
54     }
55 
56     /**
57      * A rule that applies other rules in order.
58      *
59      * @param rules all rules to be tried; the first rule that does not return
60      * {@link AlignmentRule#NO_ALIGNMENT} will define the alignment for a path; if there are no
61      * rules that return a value different from {@link AlignmentRule#NO_ALIGNMENT}, then
62      * {@link AlignmentRule#NO_ALIGNMENT} is returned
63      * @return the composition rule
64      */
compose(@onnull AlignmentRule... rules)65     public static AlignmentRule compose(@Nonnull AlignmentRule... rules) {
66         return (String path) -> {
67             for (AlignmentRule r : rules) {
68                 int align = r.alignment(path);
69                 if (align != AlignmentRule.NO_ALIGNMENT) {
70                     return align;
71                 }
72             }
73 
74             return AlignmentRule.NO_ALIGNMENT;
75         };
76     }
77 }
78