Passed
Pull Request — master (#3)
by
unknown
02:41
created

net.labymod.serverapi.Addons   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 36
Duplicated Lines 77.78 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 19
c 1
b 0
f 0
dl 28
loc 36
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D getAddons(JsonObject) 28 28 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
package net.labymod.serverapi;
2
3
import com.google.gson.JsonElement;
4
import com.google.gson.JsonObject;
5
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.UUID;
9
10
public class Addons {
0 ignored issues
show
Best Practice introduced by
This looks like a utility class. You may want to hide the implict public constructor behind a private one, so the class cannot be instantiated,
Loading history...
11
12
    /**
13
     * Parses the addons from the INFO plugin message
14
     *
15
     * @param jsonObject the json object of the message
16
     * @return a list containing the message's addons
17
     */
18 View Code Duplication
    public static List<Addon> getAddons( JsonObject jsonObject ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
19
        if ( !jsonObject.has( "addons" ) || !jsonObject.get( "addons" ).isJsonArray() )
20
            return new ArrayList<>();
21
22
        List<Addon> addons = new ArrayList<>();
23
24
        for ( JsonElement arrayElement : jsonObject.get( "addons" ).getAsJsonArray() ) {
25
            if ( !arrayElement.isJsonObject() )
26
                continue;
27
28
            JsonObject arrayObject = arrayElement.getAsJsonObject();
29
30
            if ( !arrayObject.has( "uuid" ) || !arrayObject.get( "uuid" ).isJsonPrimitive() || !arrayObject.get( "uuid" ).getAsJsonPrimitive().isString()
31
                    || !arrayObject.has( "name" ) || !arrayObject.get( "name" ).isJsonPrimitive() || !arrayObject.get( "name" ).getAsJsonPrimitive().isString() )
32
                continue;
33
34
            UUID uuid = null;
35
36
            try {
37
                uuid = UUID.fromString( arrayObject.get( "uuid" ).getAsString() );
38
            } catch ( IllegalArgumentException ex ) {
39
                continue;
40
            }
41
42
            addons.add( new Addon( uuid, arrayObject.get( "name" ).getAsString() ) );
43
        }
44
45
        return addons;
46
    }
47
}
48