Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON Pair

From Leeroopedia
Knowledge Sources
Domains Validation, Request_Processing, Type_System
Last Updated 2026-10-11 00:00 GMT

Overview

Concrete key-value pair class that parses "type:value" strings and resolves Java types from a static class map, enabling type-checked request validation in the APIJSON ORM framework.

Description

The Pair class extends Entry<String, String> and provides two core capabilities:

1. String Pair Parsing (parseEntry): Splits a "key:value" string into an Entry with left and right parts. When the colon is absent, the isRightValueDefault flag controls whether the entire string becomes the key or the value:

  • isRightValueDefault = true: string → key, value = defaultValue
  • isRightValueDefault = false: string → value, key = defaultValue

2. Variable Type Parsing (parseVariableEntry): Parses a "type:value" string into an Entry<Class<?>, Object> by:

  • Looking up the type name (e.g., "Integer", "String", "Boolean") in a static CLASS_MAP that maps primitive and wrapper type simple-names to their Class objects
  • Resolving the value from a provided valueMap using the right-hand side as the key
  • Defaulting to Object.class when no type prefix is specified

The CLASS_MAP supports: boolean, int, long, float, double, Boolean, Integer, Long, Float, Double, Object, String, Collection, Map.

This class is used by AbstractVerifier to interpret TYPE and VERIFY operation specifications from the Request configuration table — e.g., "Integer:id" means "the field id should be of type Integer".

Usage

Used internally by AbstractVerifier.verifyRequest() to parse type specifications from the Request validation table. Application code does not typically instantiate Pair directly, but may use parseEntry for general "key:value" string splitting.

Code Reference

Source Location

Signature

public class Pair extends Entry<String, String> {

    // Static class-name → Class<?> mapping for type resolution
    private static final Map<String, Class<?>> CLASS_MAP;

    public Pair();

    // Emptiness check
    public boolean isEmpty(boolean trim);

    // Correctness check
    public static <K, V> boolean isCorrect(Entry<K, V> pair);

    // Pair string formatting
    public String toPairString();
    public static String toPairString(String typeKey, String valueKey);
    public static String toPairString(Class<?> type, Object value);

    // String pair parsing — splits "key:value"
    public static Entry<String, String> parseEntry(String pair);
    public static Entry<String, String> parseEntry(String pair, boolean isRightValueDefault);
    public static Entry<String, String> parseEntry(String pair, boolean isRightValueDefault, String defaultValue);

    // Variable type parsing — resolves Class<?> and value from map
    public static Entry<String, String> parseVariableEntry(String pair);
    public static Entry<Class<?>, Object> parseVariableEntry(String pair, Map<String, Object> valueMap);
}

Import

import apijson.orm.Pair;

I/O Contract

Inputs

Name Type Required Description
pair String Yes The "type:value" or "key:value" string to parse
isRightValueDefault boolean No When no colon: true → whole string is key; false → whole string is value (default false)
defaultValue String No Default for the missing side when no colon (default null)
valueMap Map<String, Object> No Map to resolve the value from when parsing variable entries

Outputs

Name Type Description
parseEntry(...) Entry<String, String> Key-value pair split from input string
parseVariableEntry(...) Entry<Class<?>, Object> Resolved type class and value from the map
toPairString(...) String Formatted "type:value" string
isCorrect(...) boolean Whether the pair has a non-empty value
isEmpty(...) boolean Whether both key and value are empty

Usage Examples

Parsing "key:value" Strings

import apijson.orm.Pair;
import apijson.orm.Entry;

// Standard split at colon
Entry<String, String> e1 = Pair.parseEntry("Integer:id");
// e1.getKey() = "Integer", e1.getValue() = "id"

// No colon — isRightValueDefault controls behavior
Entry<String, String> e2 = Pair.parseEntry("id", true);
// e2.getKey() = "id", e2.getValue() = null

Entry<String, String> e3 = Pair.parseEntry("id", false);
// e3.getKey() = null, e3.getValue() = "id"

// With default value
Entry<String, String> e4 = Pair.parseEntry("id", true, "Object");
// e4.getKey() = "id", e4.getValue() = "Object"

Resolving Types for Request Validation

import apijson.orm.Pair;
import apijson.orm.Entry;
import java.util.Map;
import java.util.HashMap;

// Simulate how AbstractVerifier parses TYPE specifications
// Request table TYPE config: { "id": "Integer", "name": "String" }
Map<String, Object> valueMap = new HashMap<>();
valueMap.put("id", 123);
valueMap.put("name", "Tom");

// Parse "Integer:id" → resolve type and lookup value
Entry<Class<?>, Object> typed = Pair.parseVariableEntry("Integer:id", valueMap);
// typed.getKey() = Integer.class
// typed.getValue() = 123

// No type prefix → defaults to Object.class
Entry<Class<?>, Object> untyped = Pair.parseVariableEntry("id", valueMap);
// untyped.getKey() = Object.class
// untyped.getValue() = 123

Formatting Pair Strings

import apijson.orm.Pair;

// Convert type + value back to string
String s1 = Pair.toPairString(Integer.class, 42);
// → "Integer:42"

String s2 = Pair.toPairString("String", "hello");
// → "String:hello"

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment