how can i split an Elist to two Elists without getting the NullPointerException. I already tried EcoreUtil.copy() / Collections.copy. The Problem seems that when declaring the copy target List it needs to be initialized with = null; I also tried using an Iterator to copy Elements and tried adding them with .set() .add() all exit with the Exception above. The declaration of the target List seems to be working only with an allocation. While debugging i clearly see that the copied object in the List is not null.
EList<RtTask> tasks = rtModule.getTasks();
EList<RtModuleInvocation> invoc0 = null; //target List
for (RtTask rtTask : tasks) {
EList<RtModuleInvocation> invocations = rtTask.getModuleInvocations(); //src List
Thanks.
Thanks to https://www.programcreek.com/java-api-examples/emf i found out the correct way to initialize my Elist with a constructor that Creates an empty instance with no initial capacity.The data storage will be null. and HOP it works.
EList<RtModuleInvocation> invoc0 = new BasicEList<>();
If you want a copy of a list, you can also use the ECollections utility:
ECollections.newBasicEList(Iterable)
Creates a mutable BasicEList containing the given elements.
So to copy the RtModuleInvocation list you can use:
ECollections.newBasicEList(rtTask.getModuleInvocations())
Related
I have a problem regarding YAML files.
When I try moving objects from one list to another inside of a .yml file and save it, weird stuff like this happens:
queued: &id001 []
current: *id001
What's supposed to happen:
current: Fraithor # <= Minecraft username, from queued, moved when running reQueue()
queued: [] # <= Or just nothing / delete list
reQueue() method:
for (String path : FileBasics.FILETYPE.BANWAVE.getConfig().getStringList("queued")) {
List<String> list = FileBasics.FILETYPE.BANWAVE.getConfig().getStringList("current");
List<String> arrayList = new ArrayList<>();
arrayList.add(path);
list.addAll(arrayList);
FileBasics.FILETYPE.BANWAVE.getConfig().set("current", list);
list.remove(path);
FileBasics.FILETYPE.BANWAVE.getConfig().set("queued", list);
try {
FileBasics.FILETYPE.BANWAVE.getConfig().save(FileBasics.FILETYPE.BANWAVE.getFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Obviously, I'm using a seperate class/emnum for the File saving (FileBasics). It's a pretty long code, so I pasted it on pastebin HERE (https://pastebin.com/XsqvYTBb)
You're pushing the same list to both current and queued. Java has reference semantics meaning that if you push arrayList to current, current holds a reference to arrayList so if you alter it, current will point to the altered list.
Since you push the very same list to queued, YAML will add an anchor &id001 when the list first occurs, and an alias *id001 to refer to the same list afterwards.
Since you don't want a list in current at all, it seems you want to do
FileBasics.FILETYPE.BANWAVE.getConfig().set("current", path);
Though I have no idea what kind of API this is and whether it accepts a string.
I have an S3-Bucket with two files:
s3://bucketA/objectA/objectB/fileA
s3://bucketA/objectA/objectB/fileB
I want to use the s3Client in java to create a copy of objectA known as objectC using the copyObject method of s3Client.
s3://bucketA/objectA/ ---Copy-To---> s3://bucketA/objectC/
The problem is the contents of objectA are not being copied into objectC. Object C does not contain object B and fileA and fileB. How can I copy the contents of the object as well?
Here is my code: (I am using kotlin)
s3client.copyObject(CopyObjectRequest("bucketA", "objectA","bucketA", "objectC"))
I checked in the S3 console and this works (it creates a folder called objectC, but I'm unable to get the contents of objectA into object C.)
What is happening is that using the SDK you're not making a recursive copy of the objects.
So the easiest solution is to use the AWS CLI
aws s3 cp s3://source-awsexamplebucket s3://source-awsexamplebucket --recursive --storage-class STANDARD
Note that you've to take into consideration the size of the objects, the amount, etc. If its something too big a batch mechanism could be made to help your system cope with load. You can read it further on the AWS documentation.
Now, and assuming you need to be doing that programmatically. The algorithm has 2 parts listing + copying. Something along those lines will work.
ListObjectsV2Result result = s3.listObjectsV2(from_bucket);
List<S3ObjectSummary> objects = result.getObjectSummaries();
for (S3ObjectSummary os : objects) {
s3.copyObject(from_bucket, os.getKey(), to_bucket, os.getKey());
}
// exception handling and error handling ommited for brevity
I have List<CapturedImage> capturedImageList = assingedFromSomewhere();
What will happen if I delete element 0 of capturedImageList which is currently being processed by a separate Thread?
// Procesing a Thread
capturedImage.deleteImageFile(capturedImageList.get(0).getAbsolutePath());
if (capturedImage.saveToDisk(bitmap_original)) {
// Note : Thred process in not completed yet
capturedImageList.remove(0);
capturedImageList.add(0, capturedImage);
}
Will the file will get deleted?
If so, how is the object reference is working here?
I haven't tried this but i believe you will encounter a concurrent modification exception,
if you try to manipulate a resource which is already in use.. instead you can use a
CopyOnWriteArrayList
I'm getting a java.util.ConcurrentModificationException on the line where the for-loop starts (see comment in code).
Why am i getting ConcurrentModificationException on this unmodifiableSet?
final Set<Port> portSet = Collections.unmodifiableSet(node.getOpenPorts());
if (!portSet.isEmpty()) {
StringBuilder tmpSb = new StringBuilder();
for (Port pp : portSet) { // <------- exception happening here
tmpSb.append(pp.getNum()).append(" ");
}
}
I've never witnessed this, but I'm getting crash reports from Google.
Something must be modifying the underlying set; i.e. the set returned by node.getOpenPorts().
Instead of wrapping the set with an "unmodifiable" wrapper, you could copy it.
final Set<Port> portSet = new HashSet<>(node.getOpenPorts());
But as a commenter (#Slaw) pointed out, that just moves the iteration inside the constructor and you would still get CCMEs.
The only real solutions are:
Change the implementation of the node class to use a concurrent set class for the port list that won't throw CCMEs if the collection is mutated while you are iterating it.
Change the implementation of the node class to return a copy of the port list. Deal with the updates-while-copying race condition with some internal locking.
Put a try / catch around the code and repeat the operation if you get a CCME
I've never witnessed this, but I'm getting crash reports from Google.
Yes. The problem only occurs if this code is executed while the open port list is changing.
I have a sort of convoluted question. I'll try my best to explain. I am working on a Adobe CQ codebase that is JCR and Java-based.
I have a Java ArrayList declaration like this:
ArrayList<Map<String,Property>> list6 = new ArrayList<Map<String,Property>>();
Furthermore I have a linked list defined by CQ/Java code elsewhere in the code. That linked list is called linkSet6. Each 'node' in the list goes by the name links
I am trying to get the content from linkSet6 to ArrayList list6.
The problem I am facing is linkSet6 always exists but it may or may not have links in it. I am doing this and it works fine whenever there are "links"
if(currentNode != null && currentNode.hasNode("linkSet6")) {
baseNode = currentNode.getNode("linkSet6").getNode("links");
list6 = Utilities.parseStructuredMultifield(baseNode);
}
But when linkSet6 is emptyI get this error:
org.apache.sling.api.scripting.ScriptEvaluationException: An exception occurred processing JSP page /apps/citrixosd-responsive/components/content/footerNavigation/footerNavigation.jsp at line 41
Line 41 is this:
baseNode = currentNode.getNode("linkSet6").getNode("links");
I've tried size(),length() etc to check but to no avail. Any tip on how to check if links exist before doing getNode("links").
You can validate if the node exists by using Session.nodeExists(String) method.
if (session.nodeExists(currentNode.getPath + "/linkSet6")) {
...
}
The session object is usually acquired via the ResourceResolver object: resolver.adaptTo(Session.class)
Anyway - I recommend jumping into the Sling abstraction layer. Working with Resource and ResourceResolver is bit more convenient in such case (e.g. getResource will return you a null when a resource does not exist).
hasNode("links") will return true if the node links exists