Skip to content

Commit

Permalink
Simplify null handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefWN committed Apr 19, 2022
1 parent 331637d commit b882c7d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
14 changes: 7 additions & 7 deletions lib/src/coap_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class CoapMessage {
typed.Uint8Buffer? payload;

/// The size of the payload of this CoAP message.
int get payloadSize => payload == null ? 0 : payload!.length;
int get payloadSize => payload?.length ?? 0;

/// The payload of this CoAP message in string representation.
String? get payloadString {
Expand Down Expand Up @@ -830,7 +830,7 @@ class CoapMessage {
/// Content type
int get contentType {
final opt = getFirstOption(optionTypeContentType);
return opt == null ? CoapMediaType.undefined : opt.value;
return opt?.value ?? CoapMediaType.undefined;
}

set contentType(int value) {
Expand All @@ -850,7 +850,7 @@ class CoapMessage {
/// The max-age of this CoAP message.
int get maxAge {
final opt = getFirstOption(optionTypeMaxAge);
return opt == null ? CoapConstants.defaultMaxAge : opt.value;
return opt?.value ?? CoapConstants.defaultMaxAge;
}

set maxAge(int value) {
Expand All @@ -867,7 +867,7 @@ class CoapMessage {
/// Accept
int get accept {
final opt = getFirstOption(optionTypeAccept);
return opt == null ? CoapMediaType.undefined : opt.value;
return opt?.value ?? CoapMediaType.undefined;
}

set accept(int value) {
Expand Down Expand Up @@ -919,7 +919,7 @@ class CoapMessage {
/// Observe
int? get observe {
final opt = getFirstOption(optionTypeObserve);
return opt == null ? -1 : opt.value;
return opt?.value ?? -1;
}

@protected
Expand All @@ -940,7 +940,7 @@ class CoapMessage {
/// Size 1
int get size1 {
final opt = getFirstOption(optionTypeSize1);
return opt == null ? 0 : opt.value;
return opt?.value ?? 0;
}

set size1(int? value) {
Expand All @@ -954,7 +954,7 @@ class CoapMessage {
/// Size 2
int? get size2 {
final opt = getFirstOption(optionTypeSize2);
return opt == null ? 0 : opt.value;
return opt?.value ?? 0;
}

set size2(int? value) {
Expand Down
3 changes: 1 addition & 2 deletions lib/src/codec/encoders/coap_message_encoder_rfc7252.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class CoapMessageEncoderRfc7252 extends CoapMessageEncoder {
// Write fixed-size CoAP headers
writer.write(CoapRfc7252.version, CoapRfc7252.versionBits);
writer.write(message.type, CoapRfc7252.typeBits);
writer.write(message.token == null ? 0 : message.token!.length,
CoapRfc7252.tokenLengthBits);
writer.write(message.token?.length ?? 0, CoapRfc7252.tokenLengthBits);
writer.write(code, CoapRfc7252.codeBits);
writer.write(message.id, CoapRfc7252.idBits);

Expand Down
5 changes: 2 additions & 3 deletions lib/src/net/coap_exchange.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ class CoapExchange {
CoapIOutbox? _outbox;

/// Outbox
CoapIOutbox? get outbox =>
_outbox ?? (endpoint == null ? null : endpoint!.outbox);
CoapIOutbox? get outbox => _outbox ?? endpoint?.outbox;

set outbox(CoapIOutbox? value) => _outbox = value;

Expand Down Expand Up @@ -228,7 +227,7 @@ class CoapKeyToken {
class CoapKeyUri {
/// Construction
CoapKeyUri(this._uri, this._endpoint) {
_hash = _uri.hashCode * 31 + (_endpoint == null ? 0 : _endpoint.hashCode);
_hash = _uri.hashCode * 31 + (_endpoint?.hashCode ?? 0);
}

final Uri _uri;
Expand Down
9 changes: 3 additions & 6 deletions lib/src/resources/coap_resource_attributes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ class CoapResourceAttributes {
Iterable<String> get keys => _attributes.keys;

/// The resource title.
String? get title => getValues(CoapLinkFormat.title) == null
? null
: getValues(CoapLinkFormat.title)!.first;
String? get title => getValues(CoapLinkFormat.title)?.first;

set title(String? value) => set(CoapLinkFormat.title, value);

/// Gets or sets a value indicating if the resource is observable.
bool? get observable => getValues(CoapLinkFormat.observable) == null
? null
: getValues(CoapLinkFormat.observable)!.first!.isNotEmpty;
bool? get observable =>
getValues(CoapLinkFormat.observable)?.first?.isNotEmpty;

set observable(bool? value) => set(CoapLinkFormat.observable, '');

Expand Down

0 comments on commit b882c7d

Please sign in to comment.