Skip to content

Instantly share code, notes, and snippets.

@renatoapcosta
Last active August 29, 2025 03:13
Show Gist options
  • Save renatoapcosta/a0c56e9670f6160e2b4da2b27da4a3ed to your computer and use it in GitHub Desktop.
Save renatoapcosta/a0c56e9670f6160e2b4da2b27da4a3ed to your computer and use it in GitHub Desktop.

package com.exemplo;

public class Dispositivo { private String key; // ex: "DISCADO" private String label; // ex: "Modem Discado presente" private int valor; // 0 ou 1

public Dispositivo() {}

public Dispositivo(String key, String label) {
    this.key = key;
    this.label = label;
    this.valor = 0; // default
}

public String getKey() { return key; }
public void setKey(String key) { this.key = key; }

public String getLabel() { return label; }
public void setLabel(String label) { this.label = label; }

public int getValor() { return valor; }
public void setValor(int valor) { this.valor = (valor == 0 ? 0 : 1); }

}


package com.exemplo;

import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors;

public class DispositivosForm {

// UMA estrutura: chave -> Dispositivo (key, label, valor)
private final LinkedHashMap<String, Dispositivo> itens = new LinkedHashMap<>();

public DispositivosForm() {
    add(new Dispositivo("DISCADO", "Modem Discado presente"));
    add(new Dispositivo("GPRS_INT", "Modem GPRS Interno presente"));
    add(new Dispositivo("GPRS_EXT", "Modem GPRS Externo presente"));
    add(new Dispositivo("CDMA_INT", "Modem CDMA Interno presente"));
    add(new Dispositivo("CDMA_EXT", "Modem CDMA Externo presente"));
    add(new Dispositivo("ETHERNET", "Ethernet presente"));
    add(new Dispositivo("DIALUP_PPP", "Dial Up PPP presente"));
    add(new Dispositivo("WIFI_INT", "WiFi Interno presente"));
    add(new Dispositivo("WIFI_EXT", "WiFi Externo presente"));
    add(new Dispositivo("ADSL_INT", "Modem ADSL interno presente"));
    add(new Dispositivo("ADSL_EXT", "Modem ADSL Externo presente"));
    add(new Dispositivo("GPRS_DUAL_INT", "Modem GPRS Dual Chip Interno"));
    add(new Dispositivo("GPRS_DUAL_EXT", "Modem GPRS Dual Chip Externo"));
    add(new Dispositivo("LEITORA", "Leitora Código Barras e CMC7"));
    add(new Dispositivo("TECLADO_EXT", "Teclado Externo"));
    add(new Dispositivo("PINPAD_EXT", "Pinpad Externo"));
    add(new Dispositivo("MULTI_SERIAL", "Dispositivo Multi-serial"));
    add(new Dispositivo("BILHETE_UNICO", "Dispositivo Bilhete Único"));
    // → acrescente os restantes até totalizar 27 na ordem desejada
}

private void add(Dispositivo d) {
    itens.put(d.getKey(), d);
}

public LinkedHashMap<String, Dispositivo> getItens() {
    return itens;
}

/** Gera a string 0/1 na ORDEM do LinkedHashMap (27 chars). */
public String toBitString() {
    return itens.values().stream()
            .map(d -> String.valueOf(d.getValor()))
            .collect(Collectors.joining());
}

/** (Opcional) Preenche a partir de uma string 0/1 existente. */
public void fromBitString(String bits) {
    int i = 0;
    for (Dispositivo d : itens.values()) {
        if (i >= bits.length()) break;
        char c = bits.charAt(i++);
        d.setValor(c == '1' ? 1 : 0);
    }
}

/** Atalhos úteis */
public void set(String key, int valor) {
    Dispositivo d = itens.get(key);
    if (d != null) d.setValor(valor);
}
public int get(String key) {
    Dispositivo d = itens.get(key);
    return d == null ? 0 : d.getValor();
}

}


package com.exemplo;

import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*;

@Controller public class ConfigController {

@GetMapping("/config")
public String form(Model model) {
    model.addAttribute("form", new DispositivosForm());
    return "config";
}

@PostMapping("/config")
public String salvar(@ModelAttribute("form") DispositivosForm form, Model model) {
    String bitString = form.toBitString();
    model.addAttribute("bitString", bitString);
    return "config";
}

}


<title>Config</title>

Selecione os dispositivos

  <!-- checkbox 0/1 (aponta para o MESMO objeto Dispositivo) -->
  <input type="checkbox"
         th:name="${'itens[' + entry.key + '].valor'}"
         value="1"
         th:checked="${entry.value.valor == 1}" />

  <!-- rótulo vem do MESMO objeto -->
  <span th:text="${entry.value.label}"></span>
</label>

Salvar

String final (27 chars):


  <!-- checkbox envia 1 quando marcado -->
  <input type="checkbox" th:field="*{itens[__${entry.key}__].valor}" value="1"/>
  
  <!-- label -->
  <span th:text="${entry.value.label}"></span>
</label>

Salvar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment