1 /*
2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.tools.jdi;
27 
28 import com.sun.jdi.*;
29 import java.util.Collection;
30 import java.util.Iterator;
31 
32 abstract class MirrorImpl extends Object implements Mirror {
33 
34     protected VirtualMachineImpl vm;
35 
MirrorImpl(VirtualMachine aVm)36     MirrorImpl(VirtualMachine aVm) {
37         super();
38 
39         // Yes, its a bit of a hack. But by doing it this
40         // way, this is the only place we have to change
41         // typing to substitute a new impl.
42         vm = (VirtualMachineImpl)aVm;
43     }
44 
virtualMachine()45     public VirtualMachine virtualMachine() {
46         return vm;
47     }
48 
equals(Object obj)49     public boolean equals(Object obj) {
50         if ((obj != null) && (obj instanceof Mirror)) {
51             Mirror other = (Mirror)obj;
52             return vm.equals(other.virtualMachine());
53         } else {
54             return false;
55         }
56     }
57 
hashCode()58     public int hashCode() {
59         return vm.hashCode();
60     }
61 
62     /**
63      * Throw NullPointerException on null mirror.
64      * Throw VMMismatchException on wrong VM.
65      */
validateMirror(Mirror mirror)66     void validateMirror(Mirror mirror) {
67         if (!vm.equals(mirror.virtualMachine())) {
68             throw new VMMismatchException(mirror.toString());
69         }
70     }
71 
72     /**
73      * Allow null mirror.
74      * Throw VMMismatchException on wrong VM.
75      */
validateMirrorOrNull(Mirror mirror)76     void validateMirrorOrNull(Mirror mirror) {
77         if ((mirror != null) && !vm.equals(mirror.virtualMachine())) {
78             throw new VMMismatchException(mirror.toString());
79         }
80     }
81 
82     /**
83      * Throw NullPointerException on null mirrors.
84      * Throw VMMismatchException on wrong VM.
85      */
validateMirrors(Collection<? extends Mirror> mirrors)86     void validateMirrors(Collection<? extends Mirror> mirrors) {
87         Iterator<? extends Mirror> iter = mirrors.iterator();
88         while (iter.hasNext()) {
89             MirrorImpl mirror = (MirrorImpl)iter.next();
90             if (!vm.equals(mirror.vm)) {
91                 throw new VMMismatchException(mirror.toString());
92             }
93         }
94     }
95     /**
96      * Allow null mirrors.
97      * Throw VMMismatchException on wrong VM.
98      */
validateMirrorsOrNulls(Collection<? extends Mirror> mirrors)99     void validateMirrorsOrNulls(Collection<? extends Mirror> mirrors) {
100         Iterator<? extends Mirror> iter = mirrors.iterator();
101         while (iter.hasNext()) {
102             MirrorImpl mirror = (MirrorImpl)iter.next();
103             if ((mirror != null) && !vm.equals(mirror.vm)) {
104                 throw new VMMismatchException(mirror.toString());
105             }
106         }
107     }
108 }
109