All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metaeffekt.mirror.contents.msrcdata.MsThreat Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.metaeffekt.mirror.contents.msrcdata;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class MsThreat implements Comparable {

    private final String type;
    private final String productId;
    private final String description;

    public MsThreat(String type, String productId, String description) {
        this.type = type;
        this.productId = productId;
        this.description = description;
    }

    public String getProductId() {
        return productId;
    }

    public String getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }

    public JSONObject toJson() {
        final JSONObject json = new JSONObject();

        json.put("type", type);
        json.put("productId", productId);
        json.put("description", description);

        return json;
    }

    public static MsThreat fromJson(JSONObject json) {
        return new MsThreat(
                json.optString("type", null),
                json.optString("productId", null),
                json.optString("description", null)
        );
    }

    public static MsThreat fromMap(Map map) {
        return new MsThreat(
                (String) map.get("type"),
                (String) map.get("productId"),
                (String) map.get("description")
        );
    }

    public static List fromJson(JSONArray json) {
        final List threats = new ArrayList<>();

        for (int i = 0; i < json.length(); i++) {
            final JSONObject threat = json.optJSONObject(i);
            if (threat != null) {
                threats.add(fromJson(threat));
            }
        }

        return threats;
    }

    public static List fromMap(List> map) {
        final List threats = new ArrayList<>();

        for (Map threat : map) {
            if (threat != null) {
                threats.add(fromMap(threat));
            }
        }

        return threats;
    }

    public final static Comparator ID_TYPE_DESC_COMPARATOR = Comparator.comparing(MsThreat::getProductId, Comparator.nullsLast(Comparator.naturalOrder()))
            .thenComparing(MsThreat::getType, Comparator.nullsLast(Comparator.naturalOrder()))
            .thenComparing(MsThreat::getDescription, Comparator.nullsLast(Comparator.naturalOrder()));


    @Override
    public int compareTo(MsThreat o) {
        if (o == null) return 1;
        return ID_TYPE_DESC_COMPARATOR.compare(this, o);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy