summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/model/Graph.java
blob: 22b49079009f4e445b42c0b0ef444a49f2403f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
 * Copyright (c) 2017 Politecnico di Torino and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/
package it.polito.verigraph.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import it.polito.verigraph.deserializer.GraphCustomDeserializer;
import it.polito.verigraph.serializer.CustomMapSerializer;

@ApiModel(value = "Graph")
@XmlRootElement
@JsonDeserialize(using = GraphCustomDeserializer.class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Graph {

    @ApiModelProperty(required = false, hidden = true)
    @XmlTransient
    private long id;

    @ApiModelProperty(name = "nodes", notes = "Nodes", dataType = "List[it.polito.verigraph.model.Node]")
    private Map<Long, Node>nodes= new HashMap<Long, Node>();

    @ApiModelProperty(required = false, hidden = true)
    @XmlTransient
    private Set<Link>links= new HashSet<Link>();

    public Graph() {

    }

    public Graph(long id) {
        this.id = id;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @JsonSerialize(using = CustomMapSerializer.class)
    public Map<Long, Node> getNodes() {
        return nodes;
    }

    public void setNodes(Map<Long, Node> nodes) {
        this.nodes = nodes;
    }

    @XmlTransient
    public Set<Link> getLinks() {
        return links;
    }

    public void setLinks(Set<Link> links) {
        this.links = links;
    }

    public void addLink(String url, String rel) {
        Link link = new Link();
        link.setLink(url);
        link.setRel(rel);
        links.add(link);
    }

    public Node searchNodeByName(String name) {
        for (Node node : this.nodes.values()) {
            if (node.getName().equals(name))
                return node;
        }
        return null;
    }

    public int nodesWithName(String name) {
        int occurrences = 0;
        for (Node node : this.nodes.values()) {
            if (node.getName().equals(name))
                occurrences++;

        }
        return occurrences;
    }

}