DataType.java

  1. /* Copyright 2002-2022 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.gnss.metric.parser;

  18. import java.util.function.Function;

  19. /** Enum containing all low level data types that can be parsed
  20.  * to build a message.
  21.  * @author Luc Maisonobe
  22.  * @since 11.0
  23.  */
  24. enum DataType {

  25.     /** 1 bit. */
  26.     BIT_1(m -> m.extractBits(1)),

  27.     /** 2 bits. */
  28.     BIT_2(m -> m.extractBits(2)),

  29.     /** 3 bits. */
  30.     BIT_3(m -> m.extractBits(3)),

  31.     /** 4 bits. */
  32.     BIT_4(m -> m.extractBits(4)),

  33.     /** 6 bits. */
  34.     BIT_6(m -> m.extractBits(6)),

  35.     /** 7 bits. */
  36.     BIT_7(m -> m.extractBits(7)),

  37.     /** 8 bits. */
  38.     BIT_8(m -> m.extractBits(8)),

  39.     /** 10 bits. */
  40.     BIT_10(m -> m.extractBits(10)),

  41.     /** 12 bits. */
  42.     BIT_12(m -> m.extractBits(12)),

  43.     /** 24 bits. */
  44.     BIT_24(m -> m.extractBits(24)),

  45.     /** 32 bits. */
  46.     BIT_32(m -> m.extractBits(32)),

  47.     /** 6 bits signed integer. */
  48.     INT_6(m -> {
  49.         final long msb = 0x20l;
  50.         final long signed = (m.extractBits(6) ^ msb) - msb;
  51.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  52.     }),

  53.     /** 8 bits signed integer. */
  54.     INT_8(m -> {
  55.         final long msb = 0x80l;
  56.         final long signed = (m.extractBits(8) ^ msb) - msb;
  57.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  58.     }),

  59.     /** 9 bits signed integer. */
  60.     INT_9(m -> {
  61.         final long msb = 0x100l;
  62.         final long signed = (m.extractBits(9) ^ msb) - msb;
  63.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  64.     }),

  65.     /** 10 bits signed integer. */
  66.     INT_10(m -> {
  67.         final long msb = 0x200l;
  68.         final long signed = (m.extractBits(10) ^ msb) - msb;
  69.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  70.     }),

  71.     /** 11 bits signed integer. */
  72.     INT_11(m -> {
  73.         final long msb = 0x400l;
  74.         final long signed = (m.extractBits(11) ^ msb) - msb;
  75.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  76.     }),

  77.     /** 14 bits signed integer. */
  78.     INT_14(m -> {
  79.         final long msb = 0x2000l;
  80.         final long signed = (m.extractBits(14) ^ msb) - msb;
  81.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  82.     }),

  83.     /** 15 bits signed integer. */
  84.     INT_15(m -> {
  85.         final long msb = 0x4000l;
  86.         final long signed = (m.extractBits(15) ^ msb) - msb;
  87.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  88.     }),

  89.     /** 16 bits signed integer. */
  90.     INT_16(m -> {
  91.         final long msb    = 0x8000l;
  92.         final long signed = (m.extractBits(16) ^ msb) - msb;
  93.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  94.     }),

  95.     /** 17 bits signed integer. */
  96.     INT_17(m -> {
  97.         final long msb    = 0x10000l;
  98.         final long signed = (m.extractBits(17) ^ msb) - msb;
  99.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  100.     }),

  101.     /** 18 bits signed integer. */
  102.     INT_18(m -> {
  103.         final long msb    = 0x20000l;
  104.         final long signed = (m.extractBits(18) ^ msb) - msb;
  105.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  106.     }),

  107.     /** 19 bits signed integer. */
  108.     INT_19(m -> {
  109.         final long msb    = 0x40000l;
  110.         final long signed = (m.extractBits(19) ^ msb) - msb;
  111.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  112.     }),

  113.     /** 20 bits signed integer. */
  114.     INT_20(m -> {
  115.         final long msb    = 0x80000l;
  116.         final long signed = (m.extractBits(20) ^ msb) - msb;
  117.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  118.     }),

  119.     /** 21 bits signed integer. */
  120.     INT_21(m -> {
  121.         final long msb    = 0x100000l;
  122.         final long signed = (m.extractBits(21) ^ msb) - msb;
  123.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  124.     }),

  125.     /** 22 bits signed integer. */
  126.     INT_22(m -> {
  127.         final long msb    = 0x200000l;
  128.         final long signed = (m.extractBits(22) ^ msb) - msb;
  129.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  130.     }),

  131.     /** 23 bits signed integer. */
  132.     INT_23(m -> {
  133.         final long msb    = 0x400000l;
  134.         final long signed = (m.extractBits(23) ^ msb) - msb;
  135.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  136.     }),

  137.     /** 24 bits signed integer. */
  138.     INT_24(m -> {
  139.         final long msb    = 0x800000l;
  140.         final long signed = (m.extractBits(24) ^ msb) - msb;
  141.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  142.     }),

  143.     /** 25 bits signed integer. */
  144.     INT_25(m -> {
  145.         final long msb    = 0x1000000l;
  146.         final long signed = (m.extractBits(25) ^ msb) - msb;
  147.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  148.     }),

  149.     /** 26 bits signed integer. */
  150.     INT_26(m -> {
  151.         final long msb    = 0x2000000l;
  152.         final long signed = (m.extractBits(26) ^ msb) - msb;
  153.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  154.     }),

  155.     /** 27 bits signed integer. */
  156.     INT_27(m -> {
  157.         final long msb    = 0x4000000l;
  158.         final long signed = (m.extractBits(27) ^ msb) - msb;
  159.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  160.     }),

  161.     /** 30 bits signed integer. */
  162.     INT_30(m -> {
  163.         final long msb    = 0x20000000l;
  164.         final long signed = (m.extractBits(30) ^ msb) - msb;
  165.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  166.     }),

  167.     /** 31 bits signed integer. */
  168.     INT_31(m -> {
  169.         final long msb    = 0x40000000l;
  170.         final long signed = (m.extractBits(31) ^ msb) - msb;
  171.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  172.     }),

  173.     /** 32 bits signed integer. */
  174.     INT_32(m -> {
  175.         final long msb    = 0x80000000l;
  176.         final long signed = (m.extractBits(32) ^ msb) - msb;
  177.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  178.     }),

  179.     /** 34 bits signed integer. */
  180.     INT_34(m -> {
  181.         final long msb    = 0x200000000l;
  182.         final long signed = (m.extractBits(34) ^ msb) - msb;
  183.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  184.     }),

  185.     /** 35 bits signed integer. */
  186.     INT_35(m -> {
  187.         final long msb    = 0x400000000l;
  188.         final long signed = (m.extractBits(35) ^ msb) - msb;
  189.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  190.     }),

  191.     /** 38 bits signed integer. */
  192.     INT_38(m -> {
  193.         final long msb = 0x2000000000l;
  194.         final long signed = (m.extractBits(38) ^ msb) - msb;
  195.         return signed == -msb ? null : signed; // return null for the special value meaning no data
  196.     }),

  197.     /** 2 bits unsigned integer. */
  198.     U_INT_2(m -> m.extractBits(2)),

  199.     /** 3 bits unsigned integer. */
  200.     U_INT_3(m -> m.extractBits(3)),

  201.     /** 4 bits unsigned integer. */
  202.     U_INT_4(m -> m.extractBits(4)),

  203.     /** 5 bits unsigned integer. */
  204.     U_INT_5(m -> m.extractBits(5)),

  205.     /** 6 bits unsigned integer. */
  206.     U_INT_6(m -> m.extractBits(6)),

  207.     /** 7 bits unsigned integer. */
  208.     U_INT_7(m -> m.extractBits(7)),

  209.     /** 8 bits unsigned integer. */
  210.     U_INT_8(m -> m.extractBits(8)),

  211.     /** 9 bits unsigned integer. */
  212.     U_INT_9(m -> m.extractBits(9)),

  213.     /** 10 bits unsigned integer. */
  214.     U_INT_10(m -> m.extractBits(10)),

  215.     /** 11 bits unsigned integer. */
  216.     U_INT_11(m -> m.extractBits(11)),

  217.     /** 12 bits unsigned integer. */
  218.     U_INT_12(m -> m.extractBits(12)),

  219.     /** 13 bits unsigned integer. */
  220.     U_INT_13(m -> m.extractBits(13)),

  221.     /** 14 bits unsigned integer. */
  222.     U_INT_14(m -> m.extractBits(14)),

  223.     /** 16 bits unsigned integer. */
  224.     U_INT_16(m -> m.extractBits(16)),

  225.     /** 17 bits unsigned integer. */
  226.     U_INT_17(m -> m.extractBits(17)),

  227.     /** 18 bits unsigned integer. */
  228.     U_INT_18(m -> m.extractBits(18)),

  229.     /** 20 bits unsigned integer. */
  230.     U_INT_20(m -> m.extractBits(20)),

  231.     /** 23 bits unsigned integer. */
  232.     U_INT_23(m -> m.extractBits(23)),

  233.     /** 24 bits unsigned integer. */
  234.     U_INT_24(m -> m.extractBits(24)),

  235.     /** 25 bits unsigned integer. */
  236.     U_INT_25(m -> m.extractBits(25)),

  237.     /** 26 bits unsigned integer. */
  238.     U_INT_26(m -> m.extractBits(26)),

  239.     /** 27 bits unsigned integer. */
  240.     U_INT_27(m -> m.extractBits(27)),

  241.     /** 30 bits unsigned integer. */
  242.     U_INT_30(m -> m.extractBits(30)),

  243.     /** 32 bits unsigned integer. */
  244.     U_INT_32(m -> m.extractBits(32)),

  245.     /** 35 bits unsigned integer. */
  246.     U_INT_35(m -> m.extractBits(35)),

  247.     /** 36 bits unsigned integer. */
  248.     U_INT_36(m -> m.extractBits(36)),

  249.     /** 5 bits sign-magnitude integer. */
  250.     INT_S_5(m -> {
  251.         final long data = m.extractBits(5);
  252.         final long mask = -(data >>> 4); // this mask allows avoiding a conditional below
  253.         return (~mask & data) | (mask & (16l - data));
  254.     }),

  255.     /** 11 bits sign-magnitude integer. */
  256.     INT_S_11(m -> {
  257.         final long data = m.extractBits(11);
  258.         final long mask = -(data >>> 10); // this mask allows avoiding a conditional below
  259.         return (~mask & data) | (mask & (1024l - data));
  260.     }),

  261.     /** 22 bits sign-magnitude integer. */
  262.     INT_S_22(m -> {
  263.         final long data = m.extractBits(22);
  264.         final long mask = -(data >>> 21); // this mask allows avoiding a conditional below
  265.         return (~mask & data) | (mask & (2097152l - data));
  266.     }),

  267.     /** 24 bits sign-magnitude integer. */
  268.     INT_S_24(m -> {
  269.         final long data = m.extractBits(24);
  270.         final long mask = -(data >>> 23); // this mask allows avoiding a conditional below
  271.         return (~mask & data) | (mask & (8388608l - data));
  272.     }),

  273.     /** 27 bits sign-magnitude integer. */
  274.     INT_S_27(m -> {
  275.         final long data = m.extractBits(27);
  276.         final long mask = -(data >>> 26); // this mask allows avoiding a conditional below
  277.         return (~mask & data) | (mask & (67108864l - data));
  278.     }),

  279.     /** 32 bits sign-magnitude integer. */
  280.     INT_S_32(m -> {
  281.         final long data = m.extractBits(32);
  282.         final long mask = -(data >>> 31); // this mask allows avoiding a conditional below
  283.         return (~mask & data) | (mask & (2147483648l - data));
  284.     });

  285.     /** Decoding function. */
  286.     private final Function<EncodedMessage, Long> decoder;

  287.     /** Simple constructor.
  288.      * @param decoder decoding function for the data type
  289.      */
  290.     DataType(final Function<EncodedMessage, Long> decoder) {
  291.         this.decoder = decoder;
  292.     }

  293.     /** Decode a piece of data extracted from an encoded message.
  294.      * @param message encoded message providing the bits to decode
  295.      * @return data decoded as a Long object, or null if data not available
  296.      */
  297.     Long decode(final EncodedMessage message) {
  298.         return decoder.apply(message);
  299.     }

  300. }