11/*
2- * Copyright (C) 2022 github.com/REAndroid
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
15- */
2+ * Copyright (C) 2022 github.com/REAndroid
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
1616package com .reandroid .arsc .item ;
1717
1818import com .reandroid .arsc .base .Block ;
2424import com .reandroid .json .JSONConvert ;
2525import com .reandroid .json .JSONObject ;
2626import com .reandroid .utils .CompareUtil ;
27+ import com .reandroid .utils .HashSetStore ;
2728import com .reandroid .utils .ObjectsUtil ;
2829import com .reandroid .utils .collection .ComputeIterator ;
29- import com .reandroid .utils .collection .FilterIterator ;
3030import com .reandroid .xml .StyleDocument ;
3131import org .xmlpull .v1 .XmlSerializer ;
3232
3636import java .nio .charset .CharacterCodingException ;
3737import java .nio .charset .CharsetDecoder ;
3838import java .nio .charset .StandardCharsets ;
39- import java .util .HashSet ;
4039import java .util .Iterator ;
41- import java .util .Set ;
4240import java .util .function .Predicate ;
4341
4442public class StringItem extends StringBlock implements JSONConvert <JSONObject >, Comparable <StringItem > {
4543
4644 private boolean mUtf8 ;
47- private final Set < ReferenceItem > mReferencedList ;
45+ private Object mReferencedSet ;
4846 private StyleItem mStyleItem ;
4947
5048 public StringItem (boolean utf8 ) {
5149 super ();
5250 this .mUtf8 = utf8 ;
53- this .mReferencedList = new HashSet <>();
5451 }
5552
5653 public StyleDocument getStyleDocument () {
@@ -76,22 +73,21 @@ public<T extends Block> Iterator<T> getUsers(Class<T> parentClass,
7673 }
7774
7875 public void removeReference (ReferenceItem reference ) {
79- mReferencedList .remove (reference );
76+ mReferencedSet = HashSetStore .remove (mReferencedSet , reference );
8077 }
8178 public void clearReferences () {
82- mReferencedList .clear ();
79+ mReferencedSet = HashSetStore .clear (mReferencedSet );
8380 }
8481 public boolean hasReference () {
8582 ensureStringLinkUnlocked ();
86- return FilterIterator .of (mReferencedList .iterator (), referenceItem ->
87- !(referenceItem instanceof StyleItem .StyleIndexReference )).hasNext ();
83+ return HashSetStore .containsIf (mReferencedSet , NON_STYLE_INDEX );
8884 }
8985 public int getReferencesSize () {
90- return mReferencedList .size ();
86+ return HashSetStore .size (mReferencedSet );
9187 }
9288 public Iterator <ReferenceItem > getReferences () {
9389 ensureStringLinkUnlocked ();
94- return mReferencedList .iterator ();
90+ return HashSetStore .iterator (mReferencedSet );
9591 }
9692 void ensureStringLinkUnlocked () {
9793 StringPool <?> stringPool = getParentInstance (StringPool .class );
@@ -101,15 +97,17 @@ void ensureStringLinkUnlocked() {
10197 }
10298 public void addReference (ReferenceItem reference ) {
10399 if (reference != null ) {
104- mReferencedList .add (reference );
100+ mReferencedSet = HashSetStore .add (mReferencedSet , reference );
105101 int index = this .getIndex ();
106102 if (reference .get () != index ) {
107103 reference .set (index );
108104 }
109105 }
110106 }
111107 private void reUpdateReferences (int newIndex ) {
112- for (ReferenceItem reference : new HashSet <>(mReferencedList )) {
108+ Iterator <ReferenceItem > iterator = HashSetStore .clonedIterator (mReferencedSet );
109+ while (iterator .hasNext ()) {
110+ ReferenceItem reference = iterator .next ();
113111 reference .set (newIndex );
114112 }
115113 }
@@ -361,7 +359,9 @@ public void transferReferences(StringItem source) {
361359 if (getIndex () < 0 || source .getIndex () < 0 ) {
362360 return ;
363361 }
364- for (ReferenceItem reference : new HashSet <>(source .mReferencedList )) {
362+ Iterator <ReferenceItem > iterator = HashSetStore .clonedIterator (source .mReferencedSet );
363+ while (iterator .hasNext ()) {
364+ ReferenceItem reference = iterator .next ();
365365 if (isTransferable (reference )) {
366366 source .removeReference (reference );
367367 addReference (reference );
@@ -599,4 +599,7 @@ private static byte[] addBytes(byte[] bytes1, byte[] bytes2, byte[] bytes3) {
599599
600600 public static final String NAME_string = ObjectsUtil .of ("string" );
601601 public static final String NAME_style = ObjectsUtil .of ("style" );
602+
603+ private static final Predicate <Object > NON_STYLE_INDEX = item ->
604+ !(item instanceof StyleItem .StyleIndexReference );
602605}
0 commit comments