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.store;
017
018import com.agentsflex.core.util.Metadata;
019import com.agentsflex.core.document.Document;
020
021import java.util.ArrayList;
022import java.util.List;
023
024public class StoreResult extends Metadata {
025    private final boolean success;
026    private String failReason;
027    private List<Object> ids;
028
029    public StoreResult(boolean success) {
030        this.success = success;
031        this.failReason = "";
032    }
033
034    public StoreResult(boolean success, String failReason) {
035        this.success = success;
036        this.failReason = failReason == null ? "" : failReason;
037    }
038
039    public boolean isSuccess() {
040        return success;
041    }
042
043    public String getFailReason() {return failReason;}
044
045    public void setFailReason(String failReason) {this.failReason = failReason;}
046
047    public List<Object> ids() {
048        return ids;
049    }
050
051    public static StoreResult fail() {
052        return new StoreResult(false);
053    }
054
055    public static StoreResult fail(String failReason) {
056        return new StoreResult(false, failReason);
057    }
058
059    public static StoreResult success() {
060        return new StoreResult(true);
061    }
062
063    public static StoreResult successWithIds(List<Document> documents) {
064        StoreResult result = success();
065        result.ids = new ArrayList<>(documents.size());
066        for (Document document : documents) {
067            result.ids.add(document.getId());
068        }
069        return result;
070    }
071
072    @Override
073    public String toString() {
074        return "StoreResult{" +
075            "success=" + success +
076            ", ids=" + ids +
077            ", failReason='" + failReason + '\'' +
078            '}';
079    }
080}