isInitialized optimisation for reflection-based generated protos

pull/288/head
Jon Skeet 17 years ago
parent 3b3150881a
commit db9d2fcd06
  1. 34
      java/src/main/java/com/google/protobuf/GeneratedMessage.java

@ -73,6 +73,36 @@ public abstract class GeneratedMessage extends AbstractMessage {
}
return result;
}
public boolean isInitialized() {
// Check that all required fields are present.
for (FieldDescriptor field : getDescriptorForType().getFields()) {
if (field.isRequired()) {
if (!hasField(field)) {
return false;
}
}
}
// Check that embedded messages are initialized.
for (FieldDescriptor field : getDescriptorForType().getFields()) {
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
if (field.isRepeated()) {
for (Message element : (List<Message>) getField(field)) {
if (!element.isInitialized()) {
return false;
}
}
} else {
if (!((Message) getField(field)).isInitialized()) {
return false;
}
}
}
}
return true;
}
public Map<FieldDescriptor, Object> getAllFields() {
return Collections.unmodifiableMap(getAllFieldsMutable());
@ -356,6 +386,10 @@ public abstract class GeneratedMessage extends AbstractMessage {
protected boolean extensionsAreInitialized() {
return extensions.isInitialized();
}
public boolean isInitialized() {
return super.isInitialized() && extensionsAreInitialized();
}
/**
* Used by subclasses to serialize extensions. Extension ranges may be

Loading…
Cancel
Save