DiagList.java revision 3827:44bdefe64114
1/* 2 * Copyright (c) 2015, 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 26package jdk.jshell; 27 28import java.util.ArrayList; 29import java.util.Collection; 30import java.util.stream.Collectors; 31 32/** 33 * List of diagnostics, with convenient operations. 34 * 35 * @author Robert Field 36 */ 37@SuppressWarnings("serial") // serialVersionUID intentionally omitted 38final class DiagList extends ArrayList<Diag> { 39 40 private int cntNotStmt = 0; 41 private int cntUnreach = 0; 42 private int cntResolve = 0; 43 private int cntOther = 0; 44 45 DiagList() { 46 super(); 47 } 48 49 DiagList(Diag d) { 50 super(); 51 add(d); 52 } 53 54 DiagList(Collection<? extends Diag> c) { 55 super(); 56 addAll(c); 57 } 58 59 private void tally(Diag d) { 60 if (d.isError()) { 61 if (d.isUnreachableError()) { 62 ++cntUnreach; 63 } else if (d.isNotAStatementError()) { 64 ++cntNotStmt; 65 } else if (d.isResolutionError()) { 66 ++cntResolve; 67 } else { 68 ++cntOther; 69 } 70 } 71 } 72 73 @Override 74 public boolean addAll(Collection<? extends Diag> c) { 75 return c.stream().filter(this::add).count() > 0; 76 } 77 78 @Override 79 public Diag set(int index, Diag element) { 80 throw new UnsupportedOperationException(); 81 } 82 83 @Override 84 public void add(int index, Diag element) { 85 throw new UnsupportedOperationException(); 86 } 87 88 @Override 89 public boolean add(Diag d) { 90 boolean added = super.add(d); 91 if (added) { 92 tally(d); 93 } 94 return added; 95 } 96 97 @Override 98 public boolean addAll(int index, Collection<? extends Diag> c) { 99 throw new UnsupportedOperationException(); 100 } 101 102 @Override 103 public boolean remove(Object o) { 104 throw new UnsupportedOperationException(); 105 } 106 107 DiagList ofUnit(Unit u) { 108 return this.stream() 109 .filter(d -> { 110 Snippet snn = d.snippetOrNull(); 111 return snn == u.snippet(); 112 }) 113 .collect(Collectors.toCollection(DiagList::new)); 114 } 115 116 boolean hasErrors() { 117 return (cntNotStmt + cntResolve + cntUnreach + cntOther) > 0; 118 } 119 120 boolean hasResolutionErrorsAndNoOthers() { 121 return cntResolve > 0 && (cntNotStmt + cntUnreach + cntOther) == 0; 122 } 123 124 boolean hasUnreachableError() { 125 return cntUnreach > 0; 126 } 127 128 boolean hasNotStatement() { 129 return cntNotStmt > 0; 130 } 131 132 boolean hasOtherThanNotStatementErrors() { 133 return (cntResolve + cntUnreach + cntOther) > 0; 134 } 135 136} 137