1package com.xtremelabs.robolectric.shadows;
2
3import android.content.ComponentName;
4import android.content.Context;
5import android.os.Parcel;
6
7import com.xtremelabs.robolectric.internal.Implementation;
8import com.xtremelabs.robolectric.internal.Implements;
9
10import static com.xtremelabs.robolectric.Robolectric.shadowOf_;
11
12/**
13 * Shadows the {@code android.content.ComponentName} class.
14 * <p/>
15 * Just keeps track of the package and class names, and then gives them back when you ask for them.
16 */
17@SuppressWarnings({"UnusedDeclaration"})
18@Implements(ComponentName.class)
19public class ShadowComponentName {
20    private String pkg;
21    private String cls;
22
23    public void __constructor__(String pkg, String cls) {
24        if (pkg == null) throw new NullPointerException("package name is null");
25        if (cls == null) throw new NullPointerException("class name is null");
26        this.pkg = pkg;
27        this.cls = cls;
28    }
29
30    public void __constructor__(Context pkg, String cls) {
31        if (cls == null) throw new NullPointerException("class name is null");
32        this.pkg = pkg.getPackageName();
33        this.cls = cls;
34    }
35
36    public void __constructor__(Context pkg, Class<?> cls) {
37        this.pkg = pkg.getPackageName();
38        this.cls = cls.getName();
39    }
40
41    @Implementation
42    public String getPackageName() {
43        return pkg;
44    }
45
46    @Implementation
47    public String getClassName() {
48        return cls;
49    }
50
51    @Implementation
52    public static ComponentName readFromParcel(Parcel in) {
53        if (in.readInt() == 0) {
54            return null;
55        }
56        String pkg = in.readString();
57        String cls = in.readString();
58        return new ComponentName(pkg, cls);
59    }
60
61    @Implementation
62    public void writeToParcel(Parcel out, int flags) {
63        out.writeInt(1);
64        out.writeString(pkg);
65        out.writeString(cls);
66    }
67
68    @Implementation
69    public static void writeToParcel(ComponentName c, Parcel out) {
70        if (c == null) {
71            out.writeInt(0);
72        } else {
73            c.writeToParcel(out, 0);
74        }
75    }
76
77    @Override @Implementation
78    public boolean equals(Object o) {
79        if (o == null) return false;
80        o = shadowOf_(o);
81        if (o == null) return false;
82        if (this == o) return true;
83        if (getClass() != o.getClass()) return false;
84
85        ShadowComponentName that = (ShadowComponentName) o;
86
87        if (cls != null ? !cls.equals(that.cls) : that.cls != null) return false;
88        if (pkg != null ? !pkg.equals(that.pkg) : that.pkg != null) return false;
89
90        return true;
91    }
92
93    @Override @Implementation
94    public int hashCode() {
95        int result = pkg != null ? pkg.hashCode() : 0;
96        result = 31 * result + (cls != null ? cls.hashCode() : 0);
97        return result;
98    }
99
100    @Override @Implementation
101    public String toString() {
102        return "ComponentName{" +
103                "pkg='" + pkg + '\'' +
104                ", cls='" + cls + '\'' +
105                '}';
106    }
107}
108