2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.energidataservice.internal.api;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
18 * Global Location Number.
20 * The <a href="https://www.gs1.org/standards/id-keys/gln">Global Location Number (GLN)</a>
21 * can be used by companies to identify their locations.
23 * @author Jacob Laursen - Initial contribution
26 public class GlobalLocationNumber {
28 public static final GlobalLocationNumber EMPTY = new GlobalLocationNumber("");
30 private static final int MAX_LENGTH = 13;
32 private final String gln;
34 public GlobalLocationNumber(String gln) {
35 if (gln.length() > MAX_LENGTH) {
36 throw new IllegalArgumentException("Maximum length exceeded: " + gln);
42 public String toString() {
46 public boolean isEmpty() {
50 public boolean isValid() {
51 if (gln.length() != 13) {
56 for (int i = 13 - 2; i >= 0; i--) {
57 int digit = Character.getNumericValue(gln.charAt(i));
58 checksum += (i % 2 == 0 ? digit : digit * 3);
60 int controlDigit = 10 - (checksum % 10);
61 if (controlDigit == 10) {
65 return controlDigit == Character.getNumericValue(gln.charAt(13 - 1));
68 public static GlobalLocationNumber of(String gln) {
72 return new GlobalLocationNumber(gln);