2 * Copyright (c) 2010-2023 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.
19 * See {@link https://www.gs1.org/standards/id-keys/gln}}
20 * The Global Location Number (GLN) can be used by companies to identify their locations.
22 * @author Jacob Laursen - Initial contribution
25 public class GlobalLocationNumber {
27 public static final GlobalLocationNumber EMPTY = new GlobalLocationNumber("");
29 private static final int MAX_LENGTH = 13;
31 private final String gln;
33 public GlobalLocationNumber(String gln) {
34 if (gln.length() > MAX_LENGTH) {
35 throw new IllegalArgumentException("Maximum length exceeded: " + gln);
41 public String toString() {
45 public boolean isEmpty() {
49 public boolean isValid() {
50 if (gln.length() != 13) {
55 for (int i = 13 - 2; i >= 0; i--) {
56 int digit = Character.getNumericValue(gln.charAt(i));
57 checksum += (i % 2 == 0 ? digit : digit * 3);
59 int controlDigit = 10 - (checksum % 10);
60 if (controlDigit == 10) {
64 return controlDigit == Character.getNumericValue(gln.charAt(13 - 1));
67 public static GlobalLocationNumber of(String gln) {
71 return new GlobalLocationNumber(gln);