1 /**
2  * Copyright (C) 2022 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 package com.android.launcher3.util;
17 
18 /**
19  * Utility interface for representing flag operations
20  */
21 public interface FlagOp {
22 
23     FlagOp NO_OP = i -> i;
24 
apply(int flags)25     int apply(int flags);
26 
27     /**
28      * Returns a new OP which adds the provided flag after applying all previous operations
29      */
addFlag(int flag)30     default FlagOp addFlag(int flag) {
31         return i -> apply(i) | flag;
32     }
33 
34     /**
35      * Returns a new OP which removes the provided flag after applying all previous operations
36      */
removeFlag(int flag)37     default FlagOp removeFlag(int flag) {
38         return i -> apply(i) & ~flag;
39     }
40 
41     /**
42      * Returns a new OP which adds or removed the provided flag based on {@code enable} after
43      * applying all previous operations
44      */
setFlag(int flag, boolean enable)45     default FlagOp setFlag(int flag, boolean enable) {
46         return enable ? addFlag(flag) : removeFlag(flag);
47     }
48 }
49