LCOV - code coverage report
Current view: top level - extensions/client - ListOption.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 24 28 85.7 %
Date: 2022-11-19 15:00:39 Functions: 3 3 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2019 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.extensions.client;
      16             : 
      17             : import com.google.gerrit.extensions.restapi.BadRequestException;
      18             : import java.lang.reflect.InvocationTargetException;
      19             : import java.util.EnumSet;
      20             : import java.util.Set;
      21             : 
      22             : /** Enum that can be expressed as a bitset in query parameters. */
      23             : public interface ListOption {
      24             :   int getValue();
      25             : 
      26             :   static <T extends Enum<T> & ListOption> EnumSet<T> fromHexString(Class<T> clazz, String hex)
      27             :       throws BadRequestException {
      28             :     int parsed;
      29             :     try {
      30           1 :       parsed = Integer.parseInt(hex, 16);
      31           1 :     } catch (IllegalArgumentException e) {
      32           1 :       throw new BadRequestException("not a hex-encoded 32-bit integer: " + hex, e);
      33           1 :     }
      34             : 
      35             :     try {
      36           0 :       return fromBits(clazz, parsed);
      37           1 :     } catch (IllegalArgumentException e) {
      38           1 :       throw new BadRequestException(e.getMessage());
      39             :     }
      40             :   }
      41             : 
      42             :   static <T extends Enum<T> & ListOption> EnumSet<T> fromBits(Class<T> clazz, int v) {
      43           2 :     EnumSet<T> r = EnumSet.noneOf(clazz);
      44             :     T[] values;
      45             :     try {
      46             :       @SuppressWarnings("unchecked")
      47           2 :       T[] tmp = (T[]) clazz.getMethod("values").invoke(null);
      48           2 :       values = tmp;
      49           0 :     } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
      50           0 :       throw new IllegalStateException(e);
      51           2 :     }
      52           2 :     for (T o : values) {
      53           2 :       if ((v & (1 << o.getValue())) != 0) {
      54           2 :         r.add(o);
      55           2 :         v &= ~(1 << o.getValue());
      56             :       }
      57           2 :       if (v == 0) {
      58           1 :         return r;
      59             :       }
      60             :     }
      61           2 :     if (v != 0) {
      62           2 :       throw new IllegalArgumentException(
      63           2 :           "unknown " + clazz.getSimpleName() + ": " + Integer.toHexString(v));
      64             :     }
      65           0 :     return r;
      66             :   }
      67             : 
      68             :   static <T extends Enum<T> & ListOption> String toHex(Set<T> options) {
      69           1 :     int v = 0;
      70           1 :     for (T option : options) {
      71           1 :       v |= 1 << option.getValue();
      72           1 :     }
      73             : 
      74           1 :     return Integer.toHexString(v);
      75             :   }
      76             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750