001/* 002 * Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.agentsflex.core.util; 017 018import java.io.Serializable; 019import java.util.HashMap; 020import java.util.Map; 021 022public class Metadata implements Serializable { 023 024 protected Map<String, Object> metadataMap; 025 026 public Object getMetadata(String key) { 027 return metadataMap != null ? metadataMap.get(key) : null; 028 } 029 030 public Object getMetadata(String key, Object defaultValue) { 031 Object value = getMetadata(key); 032 return value != null ? value : defaultValue; 033 } 034 035 public void addMetadata(String key, Object value) { 036 if (metadataMap == null) { 037 metadataMap = new HashMap<>(); 038 } 039 metadataMap.put(key, value); 040 } 041 042 public void addMetadata(Map<String, Object> metadata) { 043 if (metadata == null || metadata.isEmpty()) { 044 return; 045 } 046 if (metadataMap == null) { 047 metadataMap = new HashMap<>(); 048 } 049 metadataMap.putAll(metadata); 050 } 051 052 public Object removeMetadata(String key) { 053 if (this.metadataMap == null) { 054 return null; 055 } 056 return this.metadataMap.remove(key); 057 } 058 059 public Map<String, Object> getMetadataMap() { 060 return metadataMap; 061 } 062 063 public void setMetadataMap(Map<String, Object> metadatas) { 064 this.metadataMap = metadatas; 065 } 066}